Skip to content

Instantly share code, notes, and snippets.

@pocki80
Last active October 18, 2019 06:44
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pocki80/525cb16b95991c7efed1c053d82422fe to your computer and use it in GitHub Desktop.
Save pocki80/525cb16b95991c7efed1c053d82422fe to your computer and use it in GitHub Desktop.
Ping-Watchdog for EdgeRouter-X: reboot on failed internet access
#!/bin/bash
# ping 8.8.8.8 every 12h and reboot on failure
# ...after 2nd try 4-14mins later to avoid bricked antennes on FW-updates!
#
# INSTALL/UPDATE via CLI:
# curl -sS https://gist.githubusercontent.com/pocki80/525cb16b95991c7efed1c053d82422fe/raw/set_ping2web.sh | sudo bash
cat >/config/scripts/ping2web.sh <<'ENDSCRIPTCONTENT'
#!/bin/bash
/bin/ping -n -c3 8.8.8.8 >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo $(date "+%b %d %H:%M:%S")" "$(hostname)" ping2web.sh: ping to 8.8.8.8 successful" >>/var/log/messages
else
echo -n $(date)" -> ping to 8.8.8.8 failed, schedule reboot: " >>/config/scripts/ping2web.log
# wait for firmware-updates long enough to complete
#at now +4min -f /config/scripts/ping2web_reboot.sh 2>/tmp/return
at now +$(($RANDOM % 10 +4))min -f /config/scripts/ping2web_reboot.sh 2>/tmp/return
tail -n1 /tmp/return >>/config/scripts/ping2web.log
rm /tmp/return
fi
exit 0
ENDSCRIPTCONTENT
sudo chmod 0755 /config/scripts/ping2web.sh
cat >/config/scripts/ping2web_reboot.sh <<'ENDSCRIPTCONTENT'
#!/bin/bash
/bin/ping -n -c3 8.8.8.8 >/dev/null 2>/dev/null
if [ $? -eq 0 ]; then
echo $(date "+%b %d %H:%M:%S")" "$(hostname)" ping2web.sh: ping to 8.8.8.8 now successful, no reboot needed" >>/var/log/messages
echo $(date)" -> ping to 8.8.8.8 now successful, no reboot needed" >>/config/scripts/ping2web.log
else
echo $(date)" -> ping to 8.8.8.8 failed again, will reboot in 15sec" >>/config/scripts/ping2web.log
sleep 15
for int in $(/usr/sbin/ubnt-ifctl show-poe-status | awk '$2=="ON"{print $1}'); do
echo "powerdown poe at $int..." >>/config/scripts/ping2web.log
/usr/sbin/ubnt-ifctl set-poe $int off >>/config/scripts/ping2web.log
done
sleep 2
echo $(date)" -> initiated reboot" >>/config/scripts/ping2web.log
/sbin/reboot >>/config/scripts/ping2web.log
fi
exit 0
ENDSCRIPTCONTENT
sudo chmod 0755 /config/scripts/ping2web_reboot.sh
if [ $(grep -A5 "task ping2web {" /config/config.boot | grep -cE "path /config/scripts/ping2web.sh|interval 12h") -ne 2 ]; then
if [ $(grep -c "task ping2web {" /config/config.boot) -ne 0 ]; then
commandlist[${#commandlist[@]}]='delete system task-scheduler task ping2web'
fi
commandlist[${#commandlist[@]}]='set system task-scheduler task ping2web executable path /config/scripts/ping2web.sh'
commandlist[${#commandlist[@]}]='set system task-scheduler task ping2web interval 12h'
cmd="/opt/vyatta/sbin/vyatta-cfg-cmd-wrapper"
tfile=$(mktemp)
(
$cmd begin
ret=0
for executecmd in "${commandlist[@]}"; do
if [ $ret == 0 ]; then
echo ""$executecmd
$cmd $executecmd || ret=1
else
echo "--skipped: "$executecmd
fi
done
if [ $ret == 0 ]; then
$cmd commit || ret=1
fi
if [ $ret == 0 ]; then
$cmd save || ret=1
fi
$cmd end
exit $ret
) >$tfile 2>&1
ret=$?
output=$(cat $tfile)
cat $tfile
fi
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment