OpenWrt WAN watchdog

(2013-10-29)

Ideally, an internet connection would be perfectly stable, but since that is not always the case, a watchdog is the next best thing. In case you ever use your home machine(s) remotely, the use case should be clear: make sure the internet connection at home still works so that you can log in when travelling.

With OpenWrt, it’s fairly easy to implement a simple watchdog that pings a public IP address every minute and triggers /etc/init.d/networking restart in case the ping fails a couple of times in a row. For a DSL connection, this means it will try to re-dial. This setup recovered a broken connection for me more than once :-).

I stored the following shell script at /root/wan-watchdog.sh:

#!/bin/sh

tries=0
while [[ $tries -lt 5 ]]
do
    if /bin/ping -c 1 8.8.8.8 >/dev/null
    then
        exit 0
    fi
    tries=$((tries+1))
done

/etc/init.d/network restart

Don’t forget to make it executable using chmod +x /root/wan-watchdog.sh.

Afterwards, add the following entry in System → Scheduled Tasks in LuCI:

* * * * * /root/wan-watchdog.sh