| 1 |
#!/opt/bin/perl |
| 2 |
|
| 3 |
use Socket; |
| 4 |
use Getopt::Long; |
| 5 |
use Net::FPing; |
| 6 |
use AnyEvent; |
| 7 |
|
| 8 |
use strict; |
| 9 |
|
| 10 |
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 |
Example: |
| 30 |
ping 10.0.0.1 .. 10.0.0.254 with at most 8 kilobytes/second and |
| 31 |
11.0.0.1 .. 11.0.0.254 as fast as possible, never exceeding 1000 packets/s, |
| 32 |
and waiting up to three seconds to wait for delayed replies: |
| 33 |
|
| 34 |
net-fping -w3 -r1000 10.0.0.1,10.0.0.254,8 11.0.0.1,11.0.0.254 |
| 35 |
EOF |
| 36 |
exit shift; |
| 37 |
} |
| 38 |
|
| 39 |
@ARGV or usage 0; |
| 40 |
|
| 41 |
my $ipv6 = eval { require Socket6; 1 }; |
| 42 |
|
| 43 |
Getopt::Long::Configure ("bundling", "no_ignore_case"); |
| 44 |
|
| 45 |
my $count = 1; |
| 46 |
my $rate = 0; |
| 47 |
my $wait = 0.25; |
| 48 |
my $quiet = 0; |
| 49 |
|
| 50 |
GetOptions ( |
| 51 |
"help|h" => sub { usage 0 }, |
| 52 |
"count|c=i" => \$count, |
| 53 |
"rate|r=f" => \$rate, |
| 54 |
"wait|w=f" => \$wait, |
| 55 |
"quiet|q" => \$quiet, |
| 56 |
) or usage 1; |
| 57 |
|
| 58 |
my @ranges; |
| 59 |
|
| 60 |
for (@ARGV) { |
| 61 |
my ($lo, $hi, $kbps) = split /,/; |
| 62 |
my $pktsz; |
| 63 |
|
| 64 |
$hi = $lo unless $hi; |
| 65 |
|
| 66 |
if ($lo =~ /:/) { |
| 67 |
# ipv6 |
| 68 |
$ipv6 or die "Socket6 module missing, no ipv6 support available.\n"; |
| 69 |
$lo = Socket6::inet_pton (&AF_INET6, $lo); |
| 70 |
$hi = Socket6::inet_pton (&AF_INET6, $hi); |
| 71 |
$pktsz = Net::FPing::icmp6_pktsize; |
| 72 |
} else { |
| 73 |
$lo = inet_aton $lo; |
| 74 |
$hi = inet_aton $hi; |
| 75 |
$pktsz = Net::FPing::icmp4_pktsize; |
| 76 |
} |
| 77 |
|
| 78 |
push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)]; |
| 79 |
} |
| 80 |
|
| 81 |
Net::FPing::register_cb { |
| 82 |
for (@{$_[0]}) { |
| 83 |
printf "%s %d %g\n", |
| 84 |
(4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])), |
| 85 |
$_->[2], |
| 86 |
$_->[1]; |
| 87 |
} |
| 88 |
} unless $quiet; |
| 89 |
|
| 90 |
for (0 .. $count - 1) { |
| 91 |
my $done = AnyEvent->condvar; |
| 92 |
Net::FPing::icmp_ping \@ranges, $rate && 1 / $rate, $_, sub { $done->broadcast }; |
| 93 |
$done->wait; |
| 94 |
} |
| 95 |
|
| 96 |
{ |
| 97 |
my $done = AnyEvent->condvar; |
| 98 |
my $wait_w = AnyEvent->timer (after => $wait, cb => sub { $done->broadcast }); |
| 99 |
$done->wait; |
| 100 |
} |
| 101 |
|
| 102 |
|