Skip to content

Instantly share code, notes, and snippets.

@MadDirtMonkey
Last active July 12, 2025 07:46
Show Gist options
  • Save MadDirtMonkey/cc794a58a3256e962488d49ec21e90ec to your computer and use it in GitHub Desktop.
Save MadDirtMonkey/cc794a58a3256e962488d49ec21e90ec to your computer and use it in GitHub Desktop.
Automatically reboot OPNsense if there is no WAN access

Overview

This is a super simple bash script that will ping Googles DNS servers every 10 minutes and if there is no response, try an interface down/up before it rebooting the machine.

NOTE: You will probably need to update the script where it says igc0 with your WAN interfaces name (you can find this easily through the GUI or by running ifconfig).

Instructions

  1. Copy the ping_check.sh file to /usr/local/etc/rc.d
  2. Make it executable with chmod 755 ping_check.sh
  3. Copy the actions_pingcheck.conf file to /usr/local/opnsense/service/conf/actions.d
  4. Reload configd so it knows about the new action with service configd restart
  5. Head to the web GUI and go to System > Settings > Cron. Add a new job, enter */10 in the minutes field and asterisks in the rest, and select your newly created "Check WAN access and reboot if offline" command.
[start]
command:/usr/local/etc/rc.d/ping_check.sh
parameters:
type:script
message:starting ping_check
description: Check WAN access and reboot if offline
#!/bin/sh
# 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 -if you don't then comment out the echo, wall and rm lines)
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 - In this case I am only using 10 seconds worth which is the -c 10 value. change this to suit.
# eg a value of 60 would be one minute.
# 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
echo "ping_check: ping fail - trying interface down/up" >> file.txt
wall file.txt
rm file.txt
ifconfig igc0 down
ifconfig igc0 up
sleep 20
echo "ping_check: ping fail - Interface reset - trying pings again." >> file.txt
wall file.txt
rm file.txt
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 && clean reboot
/usr/local/etc/rc.reboot
fi
fi
fi
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment