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