Net::Ping doesn't use ICMP, but UDP/TCP to connect to the echo port of a machine. I tried using it, but found out you couldn't use ICMP and most machines don't have their echo port enabled for obvious reasons... In my current version I use Open3. The options for ping probably don't apply to Windows ping. I use FreeBSD 4.2 here.
#!/usr/local/bin/perl5 -w
use IPC::Open3;
use Symbol;
# .ping contains hostnames, each on a seperate line.
open(HOSTS, "<.ping") || die("Can't open hostsfile! ($!)\n");
@hosts = <HOSTS>;
close(HOSTS);
%PING = ();
%STATUS = (
-1 => "Host not found",
0 => "Not done",
1 => "Host is alive",
2 => "No reply from host",
);
foreach $host (@hosts) {
chomp $host;
$WTR = gensym();
$RDR = gensym();
$ERR = gensym();
$PING{$host}{'PID'} = open3($WTR, $RDR, $ERR, "/sbin/ping -n -q -c 1
+ $host");
close($WTR);
while (<$RDR>) { $PING{$host}{'OUT'} .= $_; }
while (<$ERR>) { $PING{$host}{'ERR'} .= $_; }
$PING{$host}{'STATUS'} = 0;
}
foreach $host (keys %PING) {
if(defined($PING{$host}{'ERR'})) {
if($PING{$host}{'ERR'} =~ /Unknown host/) {
$PING{$host}{'STATUS'} = -1;
next;
}
}
if($PING{$host}{'OUT'} =~ /.*, (.*) packets received.*/) {
$PING{$host}{'STATUS'} = ($1 == 1?1:2);
}
}
foreach $host (keys %PING) {
printf "%-30s %s\n", $host, $STATUS{$PING{$host}{'STATUS'}};
}
|