From 8a89ab31eafa491d5edaa81678f627714feaf16b Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Sat, 10 Jul 2021 15:10:43 -0400 Subject: [PATCH 1/2] feat(docker): Dockerize interactive utilities tested: uap_reboot.sh --- Dockerfile | 17 +++++++++++++++++ docker_usage.sh | 16 ++++++++++++++++ uap_reboot.sh | 30 +++++++++++++++++------------- 3 files changed, 50 insertions(+), 13 deletions(-) create mode 100644 Dockerfile create mode 100755 docker_usage.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ff3c52d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,17 @@ +FROM alpine:3 + +RUN apk add --no-cache bash openjdk11-jre openssh openssl py3-pip py3-setuptools py3-wheel sshpass + +# dependencies for Nagios plugin +RUN ln -s /usr/bin/python3 /usr/bin/python +RUN pip3 install unifi + +COPY nagios/check_unifi /bin/check_unifi +COPY *.sh /bin + +RUN chmod +x /bin/check_unifi /bin/*.sh + +ENV UAP_USERNAME ubnt +ENV UAP_PASSWORD ubnt + +CMD ["docker_usage.sh"] diff --git a/docker_usage.sh b/docker_usage.sh new file mode 100755 index 0000000..1c05b95 --- /dev/null +++ b/docker_usage.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +cat <<'USAGE' +Usage: + + uap_reboot.sh: + + docker run --rm -it \ + -e UAP_USERNAME="username" \ + -e UAP_PASSWORD="password" \ + -e UAP_LIST="10.0.1.10 10.0.1.20 10.0.1.30" \ + unifi-linux-utils uap_reboot.sh + +USAGE + +exit 1 diff --git a/uap_reboot.sh b/uap_reboot.sh index b47d0f2..78a42bd 100644 --- a/uap_reboot.sh +++ b/uap_reboot.sh @@ -1,4 +1,4 @@ -#!/bin/bash +#!/usr/bin/env bash ####################################################################### # A simple script for remotely rebooting a Ubiquiti UniFi access point @@ -9,27 +9,31 @@ # which should be available via dnf, yum, or apt on your *nix distro. # # USAGE -# Update the user-configurable settings below, then run ./uap_reboot.sh from -# the command line. To reboot on a schedule, create a cronjob such as: -# 45 3 * * * /usr/local/bin/unifi-linux-utils/uap_reboot.sh > /dev/null 2>&1 #Reboot UniFi APs +# Override the user-configurable settings by providing environment variables, +# then run ./uap_reboot.sh from the command line. To reboot on a schedule, +# create a cronjob such as: +# 45 3 * * * UAP_LIST="10.0.1.10 10.0.1.20" /usr/local/bin/unifi-linux-utils/uap_reboot.sh > /dev/null 2>&1 #Reboot UniFi APs # The above example will reboot the UniFi access point(s) every morning at 3:45 AM. ####################################################################### # USER-CONFIGURABLE SETTINGS -username=ubnt -password=ubnt +username="${UAP_USERNAME:-ubnt}" +password="${UAP_PASSWORD:-ubnt}" known_hosts_file=/dev/null -uap_list=( 192.168.0.2 192.168.0.3 192.168.0.4 192.168.0.5 ) +IFS=' '; read -ra uap_list <<< "${UAP_LIST?}" # SHOULDN'T NEED TO CHANGE ANYTHING PAST HERE -for i in "${uap_list[@]}" +EXIT=0 +for i in "${!uap_list[@]}" do - echo "Rebooting UniFi access point at $i..." - if sshpass -p $password ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile=$known_hosts_file $username"@$i" reboot; then - echo "Access point at $i rebooted!" 1>&2 + uap_addr="${uap_list[$i]}" + echo "Rebooting UniFi access point at ${uap_addr}... ($((i + 1))/${#uap_list[@]})" + if sshpass -p "$password" ssh -oStrictHostKeyChecking=no -oUserKnownHostsFile="$known_hosts_file" "${username}@${uap_addr}" reboot; then + echo "Access point at $uap_addr rebooted!" 1>&2 else - echo "Could not reboot access point at $i." 1>&2 + echo "Could not reboot access point at $uap_addr." 1>&2 + EXIT=$((EXIT + 1)) fi done -exit 0 +exit $EXIT From ee35032a94a57b0ccfa259857d0cea9050607c63 Mon Sep 17 00:00:00 2001 From: Steve Huff Date: Sat, 10 Jul 2021 15:20:29 -0400 Subject: [PATCH 2/2] fix(usage): usage for Nagios check --- docker_usage.sh | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/docker_usage.sh b/docker_usage.sh index 1c05b95..5379a71 100755 --- a/docker_usage.sh +++ b/docker_usage.sh @@ -11,6 +11,14 @@ Usage: -e UAP_LIST="10.0.1.10 10.0.1.20 10.0.1.30" \ unifi-linux-utils uap_reboot.sh + check_unifi: + + docker run --rm -it \ + unifi-linux-utils check_unifi \ + -H 10.0.1.1 \ + -u "username" \ + -p "password" + USAGE exit 1