Wednesday, December 15, 2010

Check network connectivity with Perl

Sometimes it's crucial to check network connectivity to some server before running some script that will run on it, I've been searching for the easiest way to check network connectivity (ping) in Perl (without sprintf or system) and looks like I found one, the script uses the "Net::Ping" module.

The following script takes a server name as argument and verifies wether it's up or down:

#!/usr/bin/perl -w

use strict;

use warnings;

use Net::Ping;
#################################
my $master=$ARGV[0];

my $p = Net::Ping->new();

if ($p->ping($server))

{

printf "$server is up\n";

exit (0);
} else {

print"the server $server has no ping \n";

exit(0);

}

No comments: