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

Net::Ping ICMP in threads problem

Lorenz Pfiffner
June 14, 2004 08:31
Net::Ping ICMP in threads problem
Message ID:
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

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

Any suggestions, ideas?


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

my $sleeptime = 0;
sub createsubnet {
  push(@ipaddr, "", "", "", "",
"", "", "", "", "",
"", "", "", "", "",
"", "", "", "", "",
"" );
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";

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

startthreads; Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About