You also need the Time::HiRes module. I got the guts of this from somewhere
else. I modified it for my monitoring utility. This should be easy to
understand, but may not be the most efficient code.
-r
---
use Net::Ping ;
use Time::HiRes qw(tv_interval gettimeofday);
printf "Percent Success: %d, Average Response: %d",
get_ping("www.yahoo.com", 5, "icmp", 5, 1) ;
#############################################
# This subroutine expects 5 parameters:
# 1) What to ping, either a FQDN or IP address
# 2) How many times to try
# 3) What type of ping to try: TCP, UDP, ICMP
# 4) How long to wait for response, in seconds
# 5) How big should ping packet be in bytes
#############################################
# This subroutine returns two parameters:
# 1) The percent of successful ping responses
# 2) The average response time in ms
#############################################
sub get_ping(my %Data) {
my $SysToPing = shift ; # any FQDN or IP Addr
my $PingCount = shift ; # any number => 1
my $PingType = shift ; # TCP, UDP, ICMP
my $PingTimeOut = shift ; # in seconds
my $PingBytes = shift ; # bytes
my $time_elapsed = 0 ;
my $time_elapsed2 ;
my $loop_count ;
my $responses ;
my $msg = "" ;
my $p = Net::Ping->new($PingType,$PingTimeOut,$PingBytes);
my($time_start) = [gettimeofday()];
for ($loop_count = 0 ; $loop_count < $PingCount ; $loop_count++ ) {
if ( my $ping_resp = $p->ping($SysToPing) ) {
$time_elapsed2 = (tv_interval($time_start,
[gettimeofday()])) * 1000;
$time_elapsed += $time_elapsed2;
$msg = $msg."$time_elapsed2 ms, " ;
$responses += 1 ;
} else {
$time_elapsed2 = (tv_interval($time_start,
[gettimeofday()])) * 1000;
}
}
if ( $responses > 0 ) {
$time_elapsed = $time_elapsed / $responses ; # Averages
latency over those that came within timeout value
$responses = $responses / $PingCount * 100 ; # Returns as
a percentage of successful pings
} else {
$time_elapsed = 0 ;
$responses = 0 ;
}
$p->close();
return $responses, $time_elapsed;
}
---
-----Original Message-----
From: Veeraraju_Mareddi [mailto:[EMAIL PROTECTED]]
Sent: Wednesday, January 08, 2003 12:55 AM
To: '[EMAIL PROTECTED]'
Subject: Reg Net::ping
> Hi ,
>
> Our script is to get the time[Time elapsed for a byte to reach the ping
> host and to return, which is there in PING utility] from net::ping. How do
> i get this ?
>
> I dont see any finction in Net::ping. Please tell me if there is a way to
> do this. The solution can be with Net::ping / any other module.
>
> Thanks
>
> Veera
>
**************************************************************************
This email (including any attachments) is intended for the sole use of the
intended recipient/s and may contain material that is CONFIDENTIAL AND
PRIVATE COMPANY INFORMATION. Any review or reliance by others or copying or
distribution or forwarding of any or all of the contents in this message is
STRICTLY PROHIBITED. If you are not the intended recipient, please contact
the sender by email and delete all copies; your cooperation in this regard
is appreciated.
**************************************************************************
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
_______________________________________________
Perl-Win32-Admin mailing list
[EMAIL PROTECTED]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs