Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Net::Ping with threads

by Spesh00 (Initiate)
on Mar 15, 2005 at 19:22 UTC ( [id://439726]=perlquestion: print w/replies, xml ) Need Help??

Spesh00 has asked for the wisdom of the Perl Monks concerning the following question:

I need to ping 723 hosts with a single packet every minute with a 3 second time out.

Using the example in NET::Ping:

use Net::Ping; $p = Net::Ping->new(); print "$host is alive.\n" if $p->ping($host); $p->close(); $p = Net::Ping->new("icmp"); $p->bind($my_addr); # Specify source interface of pings foreach $host (@host_array) { print "$host is "; print "NOT " unless $p->ping($host, 2); print "reachable.\n"; sleep(1); } $p->close();

I can iterate through the list serially in a little over 100 seconds, what I would like to do is thread or fork this in order to speed things up. I've found a reference to a threaded "version" of the ping at:
http://www.perlmonks.org/index.pl?node_id=231514
when I altered that method to use NET::PING, the script really didn't speed up all that much. I tried altering where I created my ping object - inside the thread, outside the thread.. Print the ping as it goes, capture to an array and join back up at the end... Nothing really seems to help.

What I'm looking for is a way to heavily parallelize (sp?) my $p->ping() call in order to be able to get through the list quicker. The box it'll be running on can take it and so can the network. Any help would be greatly appreciated.

Replies are listed 'Best First'.
Re: Net::Ping with threads
by borisz (Canon) on Mar 15, 2005 at 19:28 UTC
    Here is a script that can scan around 200 hosts in 5 secs. Without threads.
    #!/usr/bin/perl use Net::Ping; my @host_array = ( '1.2.3.4', ... ); my @up; $" = "\n"; my $p = Net::Ping->new( "syn", 5 ); $p->{port_num} = 7; $p->ping($_) for (@host_array); while ( my ( $host, $rtt, $ip ) = $p->ack ) { push @up, $ip; } print @up;
    Boris
    Boris
      On activestate perl 5.8.4 on XP this doesn't return anything it seems. I did entertain syn based scanning however part of the requirements is that it also be ICMP if needed, which while being faster then straight TCP, doesn't allow the "fire and forget" nature of SYN... On the HPUX env I tried this on it wanted ICMP as well as SYN wasn't apparently supported. Excellent suggestion though, thanks, I'm seeing if I can't get it working on another env.
      I guess I should mention that this is for a windows network - running from another windows server. Developement is coming from my XP box.
      Anyone else?
Re: Net::Ping with threads
by sh1tn (Priest) on Mar 15, 2005 at 20:17 UTC
    Without threads. Class C(254 hosts) - less than a second. Perl v5.8.4; nt 5.1.2600(winxp):
    use strict; use Net::Ping; $|++; my $net = '194.153.145.'; my @hosts = 1..254; $_ = $net.$_ for @hosts; my $p = Net::Ping->new("icmp"); $p->{'timeout'} = 0.003; for (@hosts) { print "$_ is "; print "NOT " unless $p->ping($_); print "reachable.\n"; } print time - $^T


      The only real difference I'm seeing between that script that the original from the perldoc is the fact that your timeout is 0.003.. However when I run it with even with a .1 timeout value it dramatically increases the speed. I'm guessing my speed loss is directly proportional to the number of hosts that currently aren't pinging. I'm going to talk to the powers that be and see if having a subsecond timeout value is acceptable considering this is going to be more or less on the same subnet (within 4 switches or so).. The real aim of threading this system out is to prevent a host from stalling the rest of the scan. While a ping of 3 seconds would be outrageous, if the box responds it's not technically down.. However the cumulative effect of a number of boxes with a 3+ second delay causes a huge effect to the overall scan. I want to be able to catch those delays, but not when the rest of the scan could continue on merrily.
Re: Net::Ping with threads
by Steve_p (Priest) on Aug 06, 2007 at 14:14 UTC

    There was a problem with Net::Ping when using icmp pings in a thread. Essentially, there was no verification that the ping received is from the IP that the particular thread is pinging. This leads to problems that non-existant IPs are seen as being up. This problem is now fixed in the 2.33 release. Enjoy!


    Steve_p

    Test your modules with bleadperl!

      rsync -avz rsync://public.activestate.com/perl-current/ .
      ./Configure -des -Dusedevel -Dprefix=/path/to/test/perl
      make test
      make install
    

    Now, please test you modules! If you have test failures that don't happen with Perl 5.8.8, send a simplified test case to

    perlbug at perl.org

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://439726]
Approved by moot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (3)
As of 2024-04-19 21:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found