You didn't mention the operating system, but if you're using Windows, something like this may be helpful: JScript for Ping, Renew IP and Network Info / Repair.
This is a script I put together for a similar issue. It does pretty much exactly what you're looking for, in that it pings the default gateway (which will be your router) on a given schedule, and then takes required action if the ping fails.
In this case, the problem PCs didn't need to be rebooted, but simply have an
ipconfig /release and
ipconfig /renew scripted in order to regain connectivity. Something like this might work well for your case as well. If a full reboot is still required, this script could be easily modified to account for that as well.
For Linux, you could use a shell script like this:
ping -c5 192.168.1.1
if [ $? -eq 0 ]; then
This works because of the return code of
ping, as detailed in the man page:
If ping does not receive any reply packets at all it will exit with
code 1. If a packet count and deadline are both specified, and fewer
than count packets are received by the time the deadline has arrived,
it will also exit with code 1. On other error it exits with code 2.
Otherwise it exits with code 0. This makes it possible to use the exit
code to see if a host is alive or not.
192.168.1.1 with the address of your router, you could schedule this to run every hour or so using cron. In order to reboot, you'll need to run this as
root. For default Ubuntu, you can add a cron job using:
sudo crontab -e
To have this check run every hour, for example:
00 * * * * /path/to/script/pingRouterCheckReboot.sh
You'll need to ensure that the script is executable (
If you find that bringing the interface down and back up is sufficient, you could do this instead, doing something like
ifdown eth0; ifup eth0. Or, combine approaches: First do this, and then do the ping test again. If connectivity still isn't restored, then force the drastic measure of a full reboot.