Skip to content

Instantly share code, notes, and snippets.

@cmer
Last active March 9, 2023 17:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmer/20c991715c8bcbef4134a67cef2d65cc to your computer and use it in GitHub Desktop.
Save cmer/20c991715c8bcbef4134a67cef2d65cc to your computer and use it in GitHub Desktop.
Auto reboot pfSense if Internet is down
#!/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
@amiko-zz
Copy link

amiko-zz commented Apr 14, 2021

Im adding line:
set -x #echo on


[2.5.1-RELEASE]/: /usr/local/bin/ping_check.sh
+ '[[' test_internet -ne 0 ]]
/usr/local/bin/ping_check.sh: [[: not found
+ echo 'Internet is up.'
Internet is up.

Script dont working.

and:


[2.5.1-RELEASE]/: test_internet () {seconds=${1:-60} test_http http://captive.apple.com $seconds || test_http http:/                        /www.neverssl.com $seconds}
Bad : modifier in $ '-'.

@amiko-zz
Copy link

amiko-zz commented Apr 14, 2021

And that dont work too.....
Can who edit this ?


# Testing uptime to run script only xx seconds after boot

# Current time
curtime=$(date +%s)

# Bootime in seconds
uptime=$(sysctl kern.boottime | awk -F'sec = ' '{print $2}' | awk -F',' '{print $1}')

# Uptime in seconds
uptime=$(($curtime - $uptime))

# If boot is longer than 120 seconds ago...
if [ $uptime -gt 120 ]; then

# A message to the console (I like feedback)
echo "Testing Connection at" `date +%Y-%m-%d.%H:%M:%S` "uptime:" $uptime "seconds" >> file.txt
wall file.txt
rm file.txt

# Try 1 or 2 minutes worth of very short pings to googles DNS servers.
# Quit immediately if we get a single frame back.
# If neither server responds at all then reboot the firewall.

counting=$(ping -o -s 0 -c 10 8.8.8.8 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )

if [ $counting -eq 0 ]; then

counting=$(ping -o -s 0 -c 10 8.8.4.4 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )

if [ $counting -eq 0 ]; then

# trying to just restart NIC

ifconfig hn0 down
ifconfig hn0 up

counting=$(ping -o -s 0 -c 10 8.8.8.8 | grep 'received' | awk -F',' '{ print $2 }' | awk '{ print $1 }' )

if [ $counting -eq 0 ]; then

# network down
# Save RRD data

/etc/rc.backup_rrd.sh
reboot

fi
fi
fi
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment