Friday, 26 February 2010

I am running a FreeBSD server and it is connected to the Internet via a router. The router box sometimes loses the connection and needs to be manually restarted to go back on-line. So I wrote a shell script that runs on my server and notifies me if the connection goes down by beeping the speaker. The script turned out to be a nice piece of work and I decided to share it.

It works by pinging two hosts a few times and if ping is unsuccessful the beeper goes off. It runs under the bash shell and uses the beep command. As the beep varies there are two downloads, one is for UNIX and the other is for LINUX. If your OS beep syntax is different, feel free to replace the beeping part.

The script is self-explanatory and with well commented parameters. Recommended to run by cron every minute. By default it pings Google and sounds the alarm once every 5 minutes.

Another version of the same script is called ping2. It was amended to also detect DNS failures. The first host is a domain name and the second host is an IP address. The script plays one sound if the first host is down (DNS failed) and another sound if the second host is down (connection failed).


FreeBSD / UNIX 
Download ping.sh or Download ping2.sh

Ubuntu / LINUX
I lost the scripts but the only difference from above is the sound command.

Special thanks to this resource for ping parsing.

Another option is to use wget instead of ping as described here. But the ping solution is more robust and it does not store files on a drive. 

LGPL, Sergei Abramov