1 |
#!/opt/bin/perl |
2 |
|
3 |
use Getopt::Long; |
4 |
|
5 |
use AnyEvent; |
6 |
use AnyEvent::Socket; |
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 |
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
48 |
|
49 |
my $count = 1; |
50 |
my $rate = 0; |
51 |
my $wait = 0.25; |
52 |
my $quiet = 0; |
53 |
|
54 |
GetOptions ( |
55 |
"help|h" => sub { usage 0 }, |
56 |
"count|c=i" => \$count, |
57 |
"rate|r=f" => \$rate, |
58 |
"wait|w=f" => \$wait, |
59 |
"quiet|q" => \$quiet, |
60 |
) or usage 1; |
61 |
|
62 |
my @ranges; |
63 |
|
64 |
for (@ARGV) { |
65 |
my ($lo, $hi, $kbps) = split /,/; |
66 |
my $pktsz; |
67 |
|
68 |
$hi = $lo unless $hi; |
69 |
|
70 |
$lo = AnyEvent::Socket::parse_ipv6 $lo; |
71 |
$hi = AnyEvent::Socket::parse_ipv6 $hi; |
72 |
|
73 |
(length $lo) and (length $lo == length $hi) |
74 |
or die "ip adress parse error"; |
75 |
|
76 |
$pktsz = (4 == length $lo) |
77 |
? AnyEvent::FastPing::icmp4_pktsize |
78 |
: AnyEvent::FastPing::icmp6_pktsize; |
79 |
|
80 |
push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)]; |
81 |
} |
82 |
|
83 |
AnyEvent::FastPing::register_cb sub { |
84 |
for (@{$_[0]}) { |
85 |
printf "%s %d %g\n", |
86 |
AnyEvent::Socket::format_address $_->[0], |
87 |
$_->[2], |
88 |
$_->[1]; |
89 |
} |
90 |
} unless $quiet; |
91 |
|
92 |
for (1 .. $count) { |
93 |
my $done = AnyEvent->condvar; |
94 |
AnyEvent::FastPing::icmp_ping \@ranges, $rate && 1 / $rate, $_, sub { $done->broadcast }; |
95 |
$done->wait; |
96 |
} |
97 |
|
98 |
{ |
99 |
my $done = AnyEvent->condvar; |
100 |
my $wait_w = AnyEvent->timer (after => $wait, cb => sub { $done->broadcast }); |
101 |
$done->wait; |
102 |
} |
103 |
|