Hi, I am writing a script to ping a list of addresses using threads. I have included my script below.

#! /usr/bin/perl # to check ping from a file of hostnames using threads #use strict; use warnings; use threads; use threads::shared; use Net::Ping; use Time::HiRes; my $timeout = 5; my $ping_handle = Net::Ping->new("icmp",$timeout) or die "\n ping unsu +ccessful. $!";; $ping_handle->hires(); #Get the host file name and check if valid file print "\nEnter the host file name:"; chomp (my $host_file = <STDIN>); open (HOSTFILE, "<", $host_file) or die "\nOpen of $host_file failed. +$!"; #assign host_array to list of addresses in file and close my @host_array = <HOSTFILE>; close (HOSTFILE); #Get output file name and check if valid file print"\nEnter the output file name:"; chomp(my $out_file = <STDIN>); open (OUTFILE, ">", $out_file) or die "\n Open of $out_file failed.$!" +; #debugging #if($ping_handle->ping("www.google.com")){ # print "Google works\n";} #else{ # print "Ping not working\n";} my @threads; foreach $addr(@host_array){ chomp($addr); #print "$addr chomped off\n"; #debugging only my $thr = threads->new(\&sub_ping,$addr); sleep(1); push(@threads,$thr); } foreach (@threads){ my $val = $_->join(); print "Trying to ping $val complete\n"; } sub sub_ping { #check if host is a URL or an IP Address my $host= shift; #to get $ARGV[0] if (($host =~ m([a-zA-Z]+\.\w+\.\w+)) || ($host =~ m(\d{1,3}\.\d{1 +,3}\.\d{1,3}\.\d{1,3}))) { ($ret, $lat, $ip) = ($ping_handle->ping($host)); #Check if the valid host responds to pings if ($ret) { printf "$host \($ip\) responds to pings at %.2f msec.\n",$ +lat*1000; printf OUTFILE ("$host \($ip\) responds to pings at %.2f m +sec.\n",$lat*1000); } else { printf "$host is NOT responding to pings. Time Taken %.2f +msec\n",$lat*1000; printf OUTFILE ("$host is NOT responding to pings. Time ta +ken %.2f msec\n",$lat*1000); } $ping_handle->close(); } else #the address is not a URL or an IP Address { print "$host is NOT a valid address.\n"; print OUTFILE ("$host is NOT a valid address.\n"); } return $host; }

The above script works fine for all hosts which are responding. Personally if i give a host in the same network, the latency from it is a teeny tiny bit higher than what i get when i use Ping in the command line.

However, if i encounter any hosts that are not responding, then i do have a problem. Every host after that becomes NOT RESPONDING. Since its via threads, the results vary each time - sometimes its only 2 hosts not responding ( apart from the existing not responding one) sometimes its 3.

Can someone pls help me with this ?

Thanks AJ

In reply to Question on Net::Ping and Threads by werdna

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.