Friday, April 25, 2008

Ping in Perl


#!/usr/bin/perl

use Net::Ping;
use Time::HiRes;
$host = $ARGV[0];

if($host eq "") {
print "usage: ping.pl (host|ip)\n";
exit(0);
}

while(1) {
$p = Net::Ping->new();
$p->hires();
($ret, $duration, $ip) = $p->ping($host, 5.5);

printf("$host [ip: $ip] is alive (packet return time: %.2f ms)\n", 1000 * $duration) if $ret;
$p->close();
sleep 1;
}

No comments: