#!/bin/env perl use strict; use warnings; use Getopt::Long; use Net::Ping; my $host; my $count = 5; my $interval = 1; my $packetsize = 64; # remember that header data will be added to this my $timeout = 5; # 5 is the default in Net::Ping GetOptions( "h|host=s", \$host, "c|count=i", \$count, "i|interval=i", \$interval, "s|packetsize=i", \$packetsize, "W|timeout=i", \$timeout, # you had w|allowableTime but this makes more sense to me ); die "$0: No host given!\n" unless ( defined $host ); my $p = Net::Ping->new( 'tcp', # BUG: Should be able to change this with a command line option $timeout, $packetsize, ); $p->hires(); my %stats = ( received => 0, tsum => 0, max => 0, min => 9999999999 ); for ( 1 .. $count ) { my ( $ret, $duration, $ip ) = $p->ping( $host ); if ( $ret ) { $duration *= 1000; $stats{received}++; $stats{min} = $duration if ( $duration < $stats{min} ); $stats{max} = $duration if ( $duration > $stats{max} ); $stats{tsum} += $duration; printf "Your RTT is %.2f ms\n", $duration; } sleep $interval if ( defined $interval && $interval > 0 ); } if ( $stats{received} > 0 ) { $stats{avg} = $stats{tsum} / $stats{received}; } $stats{loss} = ( $count - $stats{received} ) / $count * 100; print "--- $host ping statistics ---\n"; printf "%d packets transmitted, %d received, %d%% packet loss\n", $count, $stats{received}, $stats{loss}; printf "rtt min/avg/max = %.2f/%.2f/%.2f ms\n", $stats{min}, $stats{avg}, $stats{max} if $stats{received} > 0; __DATA__ [mmusgrove@nmsdev2 mm-nms_4.5 ~]$ ./ping.pl -h 10.130.25.2 --- 10.130.25.2 ping statistics --- 5 packets transmitted, 0 received, 100% packet loss [mmusgrove@nmsdev2 mm-nms_4.5 ~]$ ./ping.pl -h 10.130.25.1 -c 8 Your RTT is 0.50 ms Your RTT is 0.37 ms Your RTT is 0.34 ms Your RTT is 0.35 ms Your RTT is 0.37 ms Your RTT is 0.36 ms Your RTT is 0.41 ms Your RTT is 0.35 ms --- 10.130.25.1 ping statistics --- 8 packets transmitted, 8 received, 0% packet loss rtt min/avg/max = 0.34/0.38/0.50 ms