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 $count = 1; |
53 |
my $quiet = 0; |
54 |
|
55 |
GetOptions ( |
56 |
"help|h" => sub { usage 0 }, |
57 |
"count|c=i" => \$count, |
58 |
"rate|r=f" => \$rate, |
59 |
"wait|w=f" => \$wait, |
60 |
"quiet|q" => \$quiet, |
61 |
) or usage 1; |
62 |
|
63 |
my @ranges; |
64 |
|
65 |
for (@ARGV) { |
66 |
my ($lo, $hi, $kbps) = split /,/; |
67 |
my $pktsz; |
68 |
|
69 |
$hi = $lo unless $hi; |
70 |
|
71 |
$lo = AnyEvent::Socket::parse_address $lo; |
72 |
$hi = AnyEvent::Socket::parse_address $hi; |
73 |
|
74 |
(length $lo) and (length $lo == length $hi) |
75 |
or die "ip adress parse error"; |
76 |
|
77 |
$pktsz = (4 == length $lo) |
78 |
? AnyEvent::FastPing::icmp4_pktsize |
79 |
: AnyEvent::FastPing::icmp6_pktsize; |
80 |
|
81 |
push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)]; |
82 |
} |
83 |
|
84 |
{ |
85 |
my $pinger = new AnyEvent::FastPing; |
86 |
|
87 |
$pinger->interval ($rate && 1 / $rate); |
88 |
$pinger->max_rtt (0); |
89 |
|
90 |
$pinger->on_recv (sub { |
91 |
for (@{ $_[0] }) { |
92 |
printf "%s %g\n", AnyEvent::Socket::format_address $_->[0], $_->[1]; |
93 |
} |
94 |
}) unless $quiet; |
95 |
|
96 |
for my $iter (1 .. $count) { |
97 |
$pinger->add_range (@$_) |
98 |
for @ranges; |
99 |
|
100 |
$pinger->on_idle (my $done = AE::cv); |
101 |
$pinger->start; |
102 |
$done->wait; |
103 |
} |
104 |
|
105 |
{ |
106 |
my $wait_w = AE::timer $wait, 0, my $done = AE::cv; |
107 |
$done->wait; |
108 |
} |
109 |
} |
110 |
|