AnyEvent-FastPing

 view release on metacpan or  search on metacpan

bin/fastping  view on Meta::CPAN


Getopt::Long::Configure ("bundling", "no_ignore_case");

my $count = 1;
my $rate  = 0;
my $wait  = 0.25;
my $count = 1;
my $quiet = 0;

GetOptions (
   "help|h"    => sub { usage 0 },
   "count|c=i" => \$count,
   "rate|r=f"  => \$rate,
   "wait|w=f"  => \$wait,
   "quiet|q"   => \$quiet,
) or usage 1;

my @ranges;

for (@ARGV) {
   my ($lo, $hi, $kbps) = split /,/;
   my $pktsz;

   $hi = $lo unless $hi;

   $lo = AnyEvent::Socket::parse_address $lo;
   $hi = AnyEvent::Socket::parse_address $hi;

   (length $lo) and (length $lo == length $hi)
      or die "ip adress parse error";

   $pktsz = (4 == length $lo)
            ? AnyEvent::FastPing::icmp4_pktsize
            : AnyEvent::FastPing::icmp6_pktsize;

   push @ranges, [$lo, $hi, $kbps && $pktsz / ($kbps * 1000)];
}

{
   my $pinger = new AnyEvent::FastPing;

   $pinger->interval ($rate && 1 / $rate);
   $pinger->max_rtt (0);

   $pinger->on_recv (sub {
      for (@{ $_[0] }) {
         printf "%s %g\n", AnyEvent::Socket::format_address $_->[0], $_->[1];
      }
   }) unless $quiet;

   for my $iter (1 .. $count) {
      $pinger->add_range (@$_)
         for @ranges;

      $pinger->on_idle (my $done = AE::cv);
      $pinger->start;
      $done->wait;
   }

   {
      my $wait_w = AE::timer $wait, 0, my $done = AE::cv;
      $done->wait;
   }
}



( run in 0.847 second using v1.01-cache-2.11-cpan-2ed5026b665 )