| 1 |
root |
1.1 |
#!/opt/bin/perl |
| 2 |
|
|
|
| 3 |
|
|
use Socket; |
| 4 |
|
|
use Getopt::Long; |
| 5 |
|
|
|
| 6 |
|
|
use AnyEvent; |
| 7 |
|
|
use AnyEvent::FastPing; |
| 8 |
|
|
|
| 9 |
|
|
use strict; |
| 10 |
|
|
|
| 11 |
|
|
sub usage { |
| 12 |
|
|
print STDERR <<EOF; |
| 13 |
|
|
Usage: fastping [-w<seconds>] [-r<packets>] [-c[count] [-q] range... |
| 14 |
|
|
|
| 15 |
|
|
--wait | -w after pinging, wait this many seconds on replies [default 0.25] |
| 16 |
|
|
--rate | -r maximum number of packets send/second [default 0, unlimited] |
| 17 |
|
|
--count | -c how many pings to send to each address [default 1] |
| 18 |
|
|
--quiet | -q do not process and output ping replies (also faster) |
| 19 |
|
|
|
| 20 |
|
|
range low[,high[,bandwidth]] |
| 21 |
|
|
low and high must be either IPv4 or IPv6 addresses, specifying |
| 22 |
|
|
a range of addresses to ping. If high is omitted, it is assumed |
| 23 |
|
|
to be equal to low. The optional bandwidth gives the IP-level |
| 24 |
|
|
maximum bandwidth in kilobytes per second. |
| 25 |
|
|
|
| 26 |
|
|
Note: |
| 27 |
|
|
* you should almost always specify a packet rate and possible range bandwidths, |
| 28 |
|
|
as the default is to ping as fast as possible. |
| 29 |
|
|
|
| 30 |
|
|
Output: |
| 31 |
|
|
For each ping reply received, net-fping will output a single line with |
| 32 |
|
|
three space-separated columns, the IP address, the iteration count and |
| 33 |
|
|
the round trip time in seconds (as a float). |
| 34 |
|
|
|
| 35 |
|
|
Example: |
| 36 |
|
|
ping 10.0.0.1 .. 10.0.0.254 with at most 8 kilobytes/second and |
| 37 |
|
|
11.0.0.1 .. 11.0.0.254 as fast as possible, never exceeding 1000 packets/s, |
| 38 |
|
|
and waiting up to three seconds to wait for delayed replies: |
| 39 |
|
|
|
| 40 |
|
|
fastping -w3 -r1000 10.0.0.1,10.0.0.254,8 11.0.0.1,11.0.0.254 |
| 41 |
|
|
EOF |
| 42 |
|
|
exit shift; |
| 43 |
|
|
} |
| 44 |
|
|
|
| 45 |
|
|
@ARGV or usage 0; |
| 46 |
|
|
|
| 47 |
|
|
my $ipv6 = eval { require Socket6; 1 }; |
| 48 |
|
|
|
| 49 |
|
|
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
| 50 |
|
|
|
| 51 |
|
|
my $count = 1; |
| 52 |
|
|
my $rate = 0; |
| 53 |
|
|
my $wait = 0.25; |
| 54 |
|
|
my $quiet = 0; |
| 55 |
|
|
|
| 56 |
|
|
GetOptions ( |
| 57 |
|
|
"help|h" => sub { usage 0 }, |
| 58 |
|
|
"count|c=i" => \$count, |
| 59 |
|
|
"rate|r=f" => \$rate, |
| 60 |
|
|
"wait|w=f" => \$wait, |
| 61 |
|
|
"quiet|q" => \$quiet, |
| 62 |
|
|
) or usage 1; |
| 63 |
|
|
|
| 64 |
|
|
my @ranges; |
| 65 |
|
|
|
| 66 |
|
|
for (@ARGV) { |
| 67 |
|
|
my ($lo, $hi, $kbps) = split /,/; |
| 68 |
|
|
my $pktsz; |
| 69 |
|
|
|
| 70 |
|
|
$hi = $lo unless $hi; |
| 71 |
|
|
|
| 72 |
|
|
if ($lo =~ /:/) { |
| 73 |
|
|
# ipv6 |
| 74 |
|
|
$ipv6 or die "Socket6 module missing, no ipv6 support available.\n"; |
| 75 |
|
|
$lo = Socket6::inet_pton (&AF_INET6, $lo); |
| 76 |
|
|
$hi = Socket6::inet_pton (&AF_INET6, $hi); |
| 77 |
|
|
$pktsz = AnyEvent::FastPing::icmp6_pktsize; |
| 78 |
|
|
} else { |
| 79 |
|
|
$lo = inet_aton $lo; |
| 80 |
|
|
$hi = inet_aton $hi; |
| 81 |
|
|
$pktsz = AnyEvent::FastPing::icmp4_pktsize; |
| 82 |
|
|
} |
| 83 |
|
|
|
| 84 |
|
|
push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)]; |
| 85 |
|
|
} |
| 86 |
|
|
|
| 87 |
root |
1.2 |
AnyEvent::FastPing::register_cb sub { |
| 88 |
root |
1.1 |
for (@{$_[0]}) { |
| 89 |
|
|
printf "%s %d %g\n", |
| 90 |
|
|
(4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])), |
| 91 |
|
|
$_->[2], |
| 92 |
|
|
$_->[1]; |
| 93 |
|
|
} |
| 94 |
|
|
} unless $quiet; |
| 95 |
|
|
|
| 96 |
|
|
for (1 .. $count) { |
| 97 |
|
|
my $done = AnyEvent->condvar; |
| 98 |
|
|
AnyEvent::FastPing::icmp_ping \@ranges, $rate && 1 / $rate, $_, sub { $done->broadcast }; |
| 99 |
|
|
$done->wait; |
| 100 |
|
|
} |
| 101 |
|
|
|
| 102 |
|
|
{ |
| 103 |
|
|
my $done = AnyEvent->condvar; |
| 104 |
|
|
my $wait_w = AnyEvent->timer (after => $wait, cb => sub { $done->broadcast }); |
| 105 |
|
|
$done->wait; |
| 106 |
|
|
} |
| 107 |
|
|
|