I'm trying to write a script that'll check to see if another machine is
pingable, and perform one of two actions based on the result. I found the
following isPingable() subroutine on a website (that is now not responding,
so I couldn't contact its author) and thought I'd see how well it works.
Right now, it behaves correctly if fed the IP of a pingable machine, or if
fed an IP that isn't in the DNS. But it delivers false positives if a
target IP is listed in the DNS but is turned off.
I'm very much a novice at scripting, so I was hoping that someone could give
me some tips on whether I've misread the subroutine and I'm feeding it
variables incorrectly, or if it's just broken and I wasn't able to pick up
on that from reading it.
D.
#!/usr/bin/perl
sub isPingable;
if ( isPingable ( "128.2.206.160" ) ) {
system ("/bin/touch pingable-YES");
} else {
system ("/bin/touch pingable-NO");
}
sub isPingable {
# isPingable -- is pingable ?
#
# version: 1.01
#
# usage: isPingable(IP_Address,[time=10sec])
#
# returns: 1 if pingable, 0 otherwise.
#
# Author: Dr. Michael M. Boyce
# Date: Tue Aug 22 00:15:08 UTC 2000
my $IP_Address=shift;
my $time = ($_[0]) ? (shift) : 10 ;
my $pid="failed";
my $count=0;
my $max_trys=10;
while (($pid=open(CHILD,"-|")) eq "failed") {
return 0 if ++$count > $max_trys;
sleep 10;
}
unless ($pid) { # child (i.e., $SIG{ALRM} Wrapper)
my $pid=open(PING,"ping -c 1 $IP_Address |") || return 0;
$SIG{ALRM} = sub {`kill -9 $pid`};
alarm $time;
my $output=pop @{[map {(/loss/) ? $_ : ()} <PING>]};
alarm 0;
close PING;
print (($output=~/0% packet loss/) ? 1 : 0) ;
exit;
}
my $pingStatus=<CHILD>;
close CHILD;
return $pingStatus;
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]