Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Problem with exit status of bash script

by NetWallah (Canon)
on Dec 04, 2014 at 06:14 UTC ( [id://1109205]=note: print w/replies, xml ) Need Help??


in reply to Problem with exit status of bash script

Here is some old code that does parallel ping using perl Threads.
#!/usr/bin/perl -w # Parallel Pinger... # Does not work on WIn32.. use strict; use threads; use Thread::Queue; use Net::Ping; my $master = $$; my $verbose = $ARGV[0] eq q(-v); my $RequestQ = Thread::Queue->new; my $ResultQ = Thread::Queue->new; my $threadcount = 20; my @kids; # Generate list of hosts to ping my @targets; foreach my $target (1..95) { #push(@targets, {IP => '10.2.56.'.$target, RESULT=>'', KIDPROC=> 0} + ); $RequestQ->enqueue ('10.2.56.' . $target); } for (0..($threadcount - 1)){ $RequestQ->enqueue(undef); push @kids, threads->new(\&Ping_it, $_); } foreach (threads->list){ $_->join; } $ResultQ->enqueue(undef); # Last item while (my $target = $ResultQ->dequeue){ print " $target\n"; } ############################################# sub Ping_it{ # my $target = shift; my ($work_count, $miss_count); my $t = shift; my $pinger = Net::Ping->new('icmp', 1); while (my $target = $RequestQ->dequeue){ $work_count ++; my $retval = 0; $verbose and print "Child $target started\n"; if($pinger->ping($target)) { # PING! Throw it in the FIFO $verbose and print "Found $target!\n"; $retval=1 } else { warn "Thread $t $target: $!\n"; $verbose and print "No response from $target\n"; $retval=0; $miss_count++; } $ResultQ->enqueue ($target . "=" . $retval); threads->yield(); $verbose and print "Child $target exiting\n"; } $pinger->close(); $ResultQ->enqueue("Thread $t processed $work_count, missed $miss_co +unt"); }

        "You're only given one little spark of madness. You mustn't lose it."         - Robin Williams

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (5)
As of 2024-04-16 18:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found