develooper Front page | perl.libnet | Postings from June 2004

Net::Ping ICMP in threads problem

From:
Lorenz Pfiffner
Date:
June 14, 2004 08:31
Subject:
Net::Ping ICMP in threads problem
Message ID:
1087227081.20039.58.camel@hype
Hi there

I'm currently fighting against a strange problem. My perl script should
do parallel pinging to a scope of addresses in a subnet. The code works
fine if I use Net::Ping with UDP or TCP. But that's not a satisfying
solution. I need ICMP because of the reachability of devices. (And less
overhead)

I think that there's a problem in connection with threads. When I do it
in a sequence without threads, the result is correct as it is if I use
UDP with threads.

The problem is, that the result is completely wrong. The pings are set
up correct, I captured icmp traffic with tcpdump. Seems that there are
values, that become overwritten again and again everytime a new thread
is started.

Now I found out that when I do a sleep before I start the thread, the
answers are also ok. So if you look in the code.. if I set $sleeptime to
higher than $pingtimeout it works. But then it is like a sequence
because it waits until the thread finished and after that it starts a
new ping subroutine.

Seems that using threads causes Net::Ping to mess up something with it's
variables?

Any suggestions, ideas?

Greetings
Lorenz


-----snip-----
 #!/usr/bin/perl -w
                         
use threads;
use Net::Ping;

my $sleeptime = 0;
                              
sub createsubnet {
  push(@ipaddr, "10.0.0.1", "10.0.0.2", "10.0.0.3", "10.0.0.4",
"10.0.0.5", "10.0.0.7", "10.0.0.8", "10.0.0.9", "10.0.0.11",
"10.0.0.12", "10.0.0.13", "10.0.0.14", "10.0.0.15", "10.0.0.16",
"10.0.0.17", "10.0.0.18", "10.0.0.19", "10.0.0.20", "10.0.0.21",
"10.0.0.22" );
}
                                                        
sub startping {
  my $host = $_;
  my $pingprotocol = "icmp";
  my $pingtimeout = 1;
  my $pinglength = 1;
                                                                                                 
  my $p = Net::Ping->new($pingprotocol,$pingtimeout,$pinglength);
    if ( $p->ping($host) ) {
      print "$host OK\n";
        } else {
          print "$host dead\n";
     }
  $p->close();
}

sub startthreads {
  foreach (@ipaddr) {
    sleep($sleeptime);
    push(@pinged_addr, threads->create("startping", $_));
  }
                                                                                                 
  foreach (@pinged_addr) { $_->join(); }
}

createsubnet;
startthreads;





nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About