#!/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 $ti
+meout
$host $ip $iterations $size $wait $total $rx $failed $Sig
+nal
$VERSION $UPDATE /;
$SIG{ INT } = sub {
$Signal++;
Stats();
exit;
};
GetOptions( 'help' => \$help, 'proto=s' => \$proto, 'count=i' => \$cou
+nt, '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 8
+0.
--proto XX Protocol to use. Options include tcp, icmp, udp, strea
+m,
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. Defaul
+t is
2000 ms (2 seconds).
--wait XX How long to wait in milliseconds between sending packe
+ts.
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 l
+oss
round-trip (ms) min/avg/max = $lo/$avg/$hi
~;
$p->close();
undef $p;
exit;
}
In reply to Ping2
by sifukurt
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.