ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/AnyEvent-FastPing/bin/net-fping
Revision: 1.6
Committed: Sat May 5 23:20:24 2007 UTC (17 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-0_02
Changes since 1.5: +33 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
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     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 root 1.1 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 root 1.3 my $wait = 0.25;
48 root 1.4 my $quiet = 0;
49 root 1.1
50     GetOptions (
51 root 1.6 "help|h" => sub { usage 0 },
52 root 1.1 "count|c=i" => \$count,
53 root 1.5 "rate|r=f" => \$rate,
54     "wait|w=f" => \$wait,
55 root 1.4 "quiet|q" => \$quiet,
56 root 1.6 ) or usage 1;
57 root 1.1
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 root 1.2 $lo = Socket6::inet_pton (&AF_INET6, $lo);
70     $hi = Socket6::inet_pton (&AF_INET6, $hi);
71     $pktsz = Net::FPing::icmp6_pktsize;
72 root 1.1 } else {
73     $lo = inet_aton $lo;
74     $hi = inet_aton $hi;
75 root 1.2 $pktsz = Net::FPing::icmp4_pktsize;
76 root 1.1 }
77    
78     push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)];
79     }
80    
81     Net::FPing::register_cb {
82 root 1.2 for (@{$_[0]}) {
83     printf "%s %d %g\n",
84 root 1.3 (4 == length $_->[0] ? inet_ntoa $_->[0] : Socket6::inet_ntop (&AF_INET6, $_->[0])),
85 root 1.2 $_->[2],
86     $_->[1];
87     }
88 root 1.4 } unless $quiet;
89 root 1.1
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