Last active
November 4, 2021 05:01
-
-
Save sgrayban/7585e8ab1d2f82339754d7cde6ebb74b to your computer and use it in GitHub Desktop.
Reboot a Raspberry Pi if LAN is unreachable
This file contains 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 | |
# | |
# Step 1) mkdir -p /root/bin | |
# Step 2) vi/nano/pico /root/bin/lan_watchdog.sh and edit the line WIFI_GW and add your own gateway IP to ping | |
# Step 3) chmod +x /root/bin/lan_watchdog.sh | |
# Step 4) Install at screen and arping `apt install screen arping at` | |
# Step 5) Add to /etc/rc.local BEFORE exit 0 | |
# # start lan_watchdog to reboot if connection is unavailable | |
# cd /root/bin/ && echo 'screen -dmS lan_watchdog /root/bin/lan_watchdog.sh' | at now+10min | |
# Step 6) Make sure everything is connected and correct and reboot the Pi | |
# | |
# NOTE: I used the screen command to launch this from rc.local so I can login from time to time and check the output of the command. | |
# | |
# preference: use arping rather than ping since ICMP can be inadvertently firewalled in netfilter | |
# limitation: host to arping must be on the same subnet | |
# ping syntax should be very similar - change arping to ping if you want to check host on different subnet | |
# function gets called to do any cleanup before rebooting | |
reboot_host() | |
{ | |
# add an entry in syslog to keep track of down times | |
logger -p0 "WIFI_GW is unreachable - going down for reboot" | |
echo "WIFI_GW is unreachable - Rebooting" | |
reboot | |
} | |
# host we should always be able to reach when wi-fi is up | |
WIFI_GW="192.168.0.1" | |
# number of seconds to wait before checking arping again | |
SLEEP_SECS=120 | |
# number of times arping is allowed to fail before a reboot | |
MAX_TIMES_DOWN=10 | |
# number of consecutive times, in loop, that WIFI_GW has been unreachable | |
down_count=0 | |
# loop forever | |
while [ 1 ]; do | |
# check using two arp packets - if arping returns non-zero status, assume it's an error | |
arping -c 2 ${WIFI_GW} | |
result=$? | |
# if ping result is non-zero, add to the down counter | |
# if result is zero (successful) reset down_count (temp network hiccup?) | |
test ${result} -ne 0 && let down_count="$down_count+1" | |
test ${result} -eq 0 && down_count=0 | |
# if arping fails in the loop MAX_TIMES_DOWN (consecutively) then reboot | |
test $down_count -gt ${MAX_TIMES_DOWN} && reboot_host | |
# wait a while before checking again | |
sleep ${SLEEP_SECS}; | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment