Last active
February 23, 2025 20:33
-
-
Save cmer/20c991715c8bcbef4134a67cef2d65cc to your computer and use it in GitHub Desktop.
Auto reboot pfSense if Internet is down
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/sh | |
# | |
# /usr/local/bin/ping_check.sh | |
# | |
# First sleep for 3 mins so that we don't run this code if box has only just booted and PPPoE not up yet. | |
/bin/sleep 180 | |
test_http() { | |
url=${1:-http://captive.apple.com} | |
seconds=${2:-300} | |
echo "Testing $url for up to $seconds seconds..." | |
curl --silent $url > /dev/null && echo "OK" && return 0 | |
sleep $seconds | |
(curl --silent $url > /dev/null && echo "OK") || (echo "FAIL" && return 1) | |
} | |
test_internet() { | |
seconds=${1:-60} | |
test_http http://captive.apple.com $seconds || test_http http://www.neverssl.com $seconds | |
} | |
flip_wan() { | |
# I have 2 WAN adapters: em0 and em2 | |
/sbin/ifconfig em0 down | |
/sbin/ifconfig em2 down | |
/bin/sleep 10 | |
/sbin/ifconfig em0 up | |
/sbin/ifconfig em2 up | |
} | |
reboot_box() { | |
/etc/rc.stop_packages | |
/etc/rc.reboot | |
} | |
if [[ test_internet -ne 0 ]]; then | |
# network down | |
# Try flipping WAN NIC in the hope that will trigger BT modem to reconnect properly ... | |
echo "Network is down. Flipping WAN NIC..." | |
flip_wan && /bin/sleep 30 | |
if [[ test_internet -ne 0 ]]; then | |
# network STILL down | |
echo "Network still down. Rebooting..." && reboot_box | |
else | |
echo "Flipping WAN restored Internet connectivity." | |
fi | |
else | |
echo "Internet is up." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
And that dont work too.....
Can who edit this ?