Last active
May 28, 2020 13:02
-
-
Save magn2o/fa698ccc067d838151b689dfe0438be7 to your computer and use it in GitHub Desktop.
Simple watchdog script for wpa_supplicant/eap_proxy (or anything, really) on the UDM-Pro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
while getopts ":h:t:n:p:s" opt; do | |
case $opt in | |
h) OPT_H="${OPTARG}" | |
;; | |
t) OPT_T=${OPTARG} | |
;; | |
n) OPT_N=${OPTARG} | |
;; | |
p) OPT_P="${OPTARG}" | |
;; | |
s) OPT_S=1 | |
esac | |
done | |
shift $(($OPTIND - 1)); | |
if [ $# -lt 2 ]; then | |
fname=$(basename "$0") | |
echo "usage: ${fname} [-s] [-h hosts] [-t timeout] [-n num_packets]" | |
printf "%-$(expr ${#fname} + 8)s" " " | |
echo "[-p password] destination [command]" | |
exit 1 | |
fi | |
SSH_DESTINATION=${1} | |
SSH_COMMAND=${@:2} | |
CHECK_HOSTS=${OPT_H:-"1.1.1.1 8.8.8.8 ping.ubnt.com"} | |
TIMEOUT_THRESHOLD=${OPT_T:-2} | |
NUM_PACKETS=${OPT_N:-1} | |
SSH_PASSWORD=${OPT_P:-""} | |
####################################################################### | |
SSH_OPTS="-q -oStrictHostKeyChecking=no -oCheckHostIP=no" | |
SHOW_OUTPUT=${OPT_S:-0} | |
TIMEOUTS=0 | |
####################################################################### | |
for host in ${CHECK_HOSTS}; do | |
if ! ping -c ${NUM_PACKETS} $host &> /dev/null; then | |
((TIMEOUTS=TIMEOUTS+1)) | |
echo "`date` : WARN : Request timed out for ${host}" | |
[ "${TIMEOUTS}" -eq "${TIMEOUT_THRESHOLD}" ] && break | |
else | |
echo "`date` : NOTICE : Ping response received from ${host}" | |
fi | |
done | |
if [ "${TIMEOUTS}" -lt "${TIMEOUT_THRESHOLD}" ]; then | |
exit 0 | |
fi | |
echo "`date` : NOTICE : Max timeout threshold reached (${TIMEOUTS}), executing: \"${SSH_COMMAND}\" on remote system" | |
expect <<DONE | |
set timeout -1 | |
log_user 0 | |
spawn -noecho ssh $SSH_OPTS "${SSH_DESTINATION}" "${SSH_COMMAND}" | |
send -- "\r" | |
expect "*?assword:*" | |
send -- "${SSH_PASSWORD}\r" | |
log_user "${SHOW_OUTPUT}" | |
expect eof | |
DONE |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment