#!/usr/bin/perl #-------------------------------------------------------------------------# # Ping2 # Date Written: 11-Feb-2005 07:54:34 PM # Last Modified: 14-Feb-2005 04:26:29 PM # Author: Kurt Kincaid # Copyright (c) 2005, Kurt Kincaid # All Rights Reserved # This script may be modified and/or redistributed # under the same terms as Perl itself. # #-------------------------------------------------------------------------# use Net::Ping; use Getopt::Long; use Time::HiRes qw/ usleep /; use Socket; use strict; use vars qw/ $help $proto $count $wait $p $ret $duration $ip $port $timeout $host $ip $iterations $size $wait $total $rx $failed $Signal $VERSION $UPDATE /; $SIG{ INT } = sub { $Signal++; Stats(); exit; }; GetOptions( 'help' => \$help, 'proto=s' => \$proto, 'count=i' => \$count, 'timeout=i' => \$timeout, 'wait=i' => \$wait, 'size=i' => \$size, 'port=i' => \$port ); $VERSION = "1.1"; $UPDATE = "14-Feb-2005 04:26:29 PM"; $proto ||= "tcp"; $wait ||= 1000; $size ||= 64; $port ||= 80; $timeout ||= 2; $host = $ARGV[ -1 ]; $wait *= 1000; if ( $size > 1024 ) { $size = 1024 } unless( $host ) { $help = 1; } if ( $help ) { print qq~Ping2 v$VERSION ($UPDATE) Usage ping2 [OPTION]... HOST Options: --count NUM Number of packets to send. --help This help text. --port XX Port to which connection attempt is made. Default is 80. --proto XX Protocol to use. Options include tcp, icmp, udp, stream, and syn. Default is tcp. --size SIZE Size, in bytes, of packet to send. Maximum is 1024. Default is 64. --timeout XX Timeout in milliseconds to wait for each reply. Default is 2000 ms (2 seconds). --wait XX How long to wait in milliseconds between sending packets. Default is 1000 ms (1 second). Report bugs to Kurt Kincaid (sifukurt\@yahoo.com) ~; exit; } if ( $host =~ /[a-zA-Z]/ ) { my $resp = inet_aton( $host ); if ( $resp ) { $ip = inet_ntoa( $resp ); } do { print "Unable to resolve hostname.\n"; exit; } unless $ip; } else { $ip = $host; } $p = Net::Ping->new( $proto, $timeout, $size ); $p->hires(); $p->{ port_num } = $port; my( $hi, $lo ); $rx = 0; print( "Pinging $host ($ip) on port $port: $size bytes of data.\n" ); while( 1 ) { if ( $Signal ) { exit; } ( $ret, $duration, $ip ) = $p->ping( $host ); $iterations++; $duration *= 1000; if ( $ret ) { if ( $hi eq "" && $lo eq "" ) { $hi = $duration; $lo = $duration; } if ( $duration > $hi ) { $hi = $duration; } if ( $duration < $lo ) { $lo = $duration; } $total += $duration; $rx++; my $dur = sprintf( "%.5f", $duration ); print( "$size bytes from $ip: seq=$iterations time=$dur ms\n" ); } else { $failed++; print( "Request timed out.\n" ); } if ( $iterations == $count ) { Stats() } usleep( $wait ); } sub Stats { $Signal++; local $SIG{ __DIE__ } = ''; my( $per, $avg, $time ); $per = sprintf( "%.2f", ( $failed / $iterations ) * 100 ); if ( $rx ) { $avg = $total / $rx; } else { $avg = 0; } $avg = sprintf( "%.5f", $avg ); $time = sprintf( "%.5f", $total ); $hi = sprintf( "%.5f", $hi ); $lo = sprintf( "%.5f", $lo ); print qq~--- $ip ping2 statistics --- $iterations packets transmitted, $rx packets received, $per\% packet loss round-trip (ms) min/avg/max = $lo/$avg/$hi ~; $p->close(); undef $p; exit; }