pfSense Interface Reboot Script
Posted on Sun 10 June 2018 in pfSense
I've been having an issue with my PiCams. They'll loose connection to the network and not come back up until the interface is restarted.
That's annoying.
They continue to do their thing locally, so pictures are eventually uploaded but remote access, via VPN and local web access stop working.
That too is annoying.
I know for a fact it's not the cameras themselves, when I start/stop the interface in pfSense they resume within seconds.
My pfSense wifi is sketchy at best.
I figured I needed a script to reboot the interface should a ping to one of the cams fail. I've never written a script for pfSense before. I wasn't entirely sure it was possible. After a little bit of Googlemasing and a well duh moment, (its FreeBSD, of course you can script with it!), I came up with the below. Credits to the below pages for making it possible.
Benn Tech https://forum.netgate.com/topic/16217/howto-ping-hosts-and-reset-reboot-on-failure.
kilko https://forum.netgate.com/topic/64563/pfsense-auto-reboot-script-when-google-is-unreachable.
1. Enable SSH via the web GUI
a. Advanced > Tick SSH
2. Using a terminal emulator, Putty in my case, login to your pfSense via SSH
a. Hit 8 for shell command
3. To remount file systems as read-write, run: /etc/rc.conf_mount_rw
4. Use vi editor to create /usr/local/bin/ping-check.sh
a. To create file; vi ping-check.sh, click “i” and paste the code, click “esc”, type “:wq!”
5. chmod 700 ping-check.sh
6. mount as read-only again, run: /etc/rc.conf_mount_ro
7. Exit
8. Disable SSH via the GUI.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | #!/bin/sh
#=====================================================================
# pingtest.sh, v1.0.1
# Created 2009 by Bennett Lee
# Released to public domain
#
# (1) Attempts to ping several hosts to test connectivity. After
# first successful ping, script exits.
# (2) If all pings fail, resets interface and retries all pings.
# (3) If all pings fail again after reset, then reboots pfSense.
#
# History
# 1.0.1 Added delay to ensure interface resets (thx ktims).
# 1.0.0 Initial release.
#=====================================================================
#=====================================================================
# USER SETTINGS
#
# Set multiple ping targets separated by space. Include numeric IPs
# (e.g., remote office, ISP gateway, etc.) for DNS issues which
# reboot will not correct.
ALLDEST="192.168.10.100 192.168.10.101 192.168.10.102"
# Interface to reset, usually your WAN
BOUNCE=ath0_wlan0
# Log file
LOGFILE=/root/pingtest.log
#=====================================================================
COUNT=1
while [ $COUNT -le 2 ]
do
for DEST in $ALLDEST
do
#echo `date +%Y%m%d.%H%M%S` "Pinging $DEST" >> $LOGFILE
ping -c1 $DEST >/dev/null 2>/dev/null
if [ $? -eq 0 ]
then
#echo `date +%Y%m%d.%H%M%S` "Ping $DEST OK." >> $LOGFILE
exit 0
fi
done
if [ $COUNT -le 1 ]
then
echo `date +%Y%m%d.%H%M%S` "All pings failed. Resetting interface $BOUNCE." >> $LOGFILE
/sbin/ifconfig $BOUNCE down
# Give interface time to reset before bringing back up
sleep 10
/sbin/ifconfig $BOUNCE up
# Give WAN time to establish connection
sleep 60
else
echo `date +%Y%m%d.%H%M%S` "All pings failed twice. Rebooting..." >> $LOGFILE
/sbin/shutdown -r now >> $LOGFILE
exit 1
fi
COUNT=`expr $COUNT + 1`
done
|
9. Install Cron from packages
a. Package Manager > Cron
10. Run the script every 5 mins
a. Click Add
b. minute: 5, * for everything else
c. user: root
d. command: /usr/local/bin/ping-check.sh
At then tested everything by: (I have a web page with all cams refreshing every 5 seconds)
Pinging two of the cams -t
Powering off one of them (the one the script is looking for)
watched as one cam screen stopped
Ran the script
Waited for the pings to fail
watched as all cam screens stopped
Powered back on cam
watched all cam screens resume
Tested, working. Quite pleased with this one.