#!/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, tsum2 => 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; $stats{tsum2} += $duration * $duration; printf "Your RTT is %.3f ms\n", $duration; } sleep $interval if ( defined $interval && $interval > 0 ); } if ( $stats{received} > 0 ) { $stats{avg} = $stats{tsum} / $stats{received}; $stats{avg2} = $stats{tsum2} / $stats{received}; $stats{mdev} = sqrt( $stats{avg2} - $stats{avg} * $stats{avg} ); } $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/mdev = %.3f/%.3f/%.3f/%.3f ms\n", $stats{min}, $stats{avg}, $stats{max}, $stats{mdev} if $stats{received} > 0;