2011.01.26

Perl

I needed this script last week… See, our ISP had a bit of downtime, roughly 7 hours of downtime actually, but throughout the day there were some periods where our connection would be up for a few minutes before it went down again.

#!/usr/bin/perl

use Net::Ping;

$p = Net::Ping->new("tcp", 2);
$p->{port_num} = getservbyname("http", "tcp");
$host = 'www.google.com';

while (1) {
if ($p->ping($host)) {
print "Internet is up!\n";
# the next two lines will only be useful on osx
$cmd = 'say -v Victoria "Internet is up"';
system ($cmd);
}
else {
print "...\n";
}
sleep(5);
}

To alert me to these few golden minutes of Internet connectivity I whipped up this script. It keeps trying to reach some server (in this example I’m using the highly-available www.google.com) waiting just 5 seconds between each check, and if it reaches the server (meaning our Internet connection is up) it prints “Internet is up!” and on Mac OS X it also says “Internet is up.” I made it say that so I could keep staring at what I was working on and be alerted audibly when the connection returned.

I was tempted to run this on our Mac jukebox so it would announce to the entire office when the connection was up, but by that time things seemed back to normal.

Obviously you could reverse this code to check if a server is up instead of down, and in fact one of my monitoring systems does just that. If you’re ever at the 2XL Networks office and hear “Attention! The server is not responding!” being yelled from one of the Macs… you know there’s a problem.

Comments are closed.

« | »


buy the button:

Buy The Button