hi, Have a look at the module Net::Ping for more options.
here's a sample script that pings a host once and prints the result.
#!/usr/bin/perl -w
use strict;
use English;
use Net::Ping;
my $host = "<hostname_goes_here>";
my $p = Net::Ping->new("icmp");
my $val = $p->ping($host);
if(!($val)) {
my @arr = localtime();
my $date = sprintf("%04d/%02d/%02d-%02d:%02d:%02d",$arr[5]+1900,$arr[4]+ 1,$arr[3],$arr[2],$arr[1],$arr[0]);
print "Ping Failed at $date\n"; # Prints in the form of "YYYY/MM/DD-HH:MM:SS.
}else {
print "Could ping successfully for host $host\n";
}
$p->close();
or another simpler way is:
#!/usr/bin/perl -w
use strict;
use English;
my @cmd = `ping <hostname_goes_here>`;
if(grep(/Unknown|Destination host unreachable/,@cmd)) {
my @arr = localtime();
my $date = sprintf("%04d/%02d/%02d-%02d:%02d:%02d",$arr[5]+1900,$arr[4]+ 1,$arr[3],$arr[2],$arr[1],$arr[0]);
print "Could not ping at $date\n";
} else {
print "Could ping successfully\n";
}
--Prasanna
Ben Crane wrote:
Hi all,
I need to set the ping command running for a few hours
on my machine, and record (the exact time/date) then
the ping function fails.
I have tried putting a script together but no joy. Does anyone have a script like this that I could please "borrow"?? I need to run the checks this afternoon and Wednesday and I've tried rather unsuccessfully to time ping responses from ms-dos (but if anyone knows how to do that then that'll be fine)
Thank you,
Ben
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com
-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>
