Codebox Software

Linux Firewall Reboot Script

Published:

This script attempts to ping a list of well-known internet hosts, and if it fails to contact any of them will reboot the system. I have used this with great success on my IPCop firewall, which occasionally drops its internet connection and will reconnect if rebooted. Schedule this using cron to perform a connectivity check every 15 minutes.

#!/bin/sh
HOSTS="www.google.com www.bbc.co.uk www.yahoo.com"
FOUND=0
for thishost in $HOSTS; do
    ping -c $thishost
    if [[ $? -eq 0 ]]; then
        FOUND=1
    fi
done
if [[ FOUND -ne 1 ]]; then
    reboot
fi

Notes

At the time of writing, the 3 hosts in the HOSTS list all respond to pings, and have done for a number of years. Add others, or change them if you wish, but just bear in mind that not every host is so obliging (www.msn.com and www.microsoft.com don't seem to respond for example).