Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

I needed a script that launched three instancies of the nmap network scanner to scan all our network in parallel. Each time a subnet is scanned and nmap finishes, another instance gets started to scan another subnet. This way I always have three parallel processes that do the job.

To learn how to do it I created this small script that could be a starting point for accomplishing similar tasks. You can substitute the sleep with an exec that runs an external program, for example, or fill that space with any perl you like!

Update: Thanks to other monks' suggestions I added "sanity checks": now the return value from fork and wait is checked. Thanks also to larsen, valdez, abell and alloalex's for their suggestions

#!/usr/bin/perl # forktest.pl # # I needed a script that launched three instancies of the nmap # network scanner to scan all our network in parallel. Each time # a subnet is scanned and nmap finishes, another instance gets # started to scan another subnet. This way I always have three # parallel processes that do the job. # # To learn how to do it I created this small script that could # be a starting point for accomplishing similar tasks. use strict ; use warnings ; my %childpid ; my $maxchild = 3 ; my $subnet = 1 ; do { while (keys(%childpid) < $maxchild and $subnet <= 254) { my $pid = fork ; # The following instruction comes from node 237098 die "Cannot fork: $!" unless defined $pid ; if ($pid) { # Parent process print "Child $pid started to scan subnet $subnet\n" ; $childpid{$pid} = $subnet++ ; } else { # Child process sleep rand 5 ; exit ; } } my $dead = wait ; # This die added after merlyn's suggestion # If you want to check a waitpid solution, see the code # posted by merlyn and zentara die "Something weird happened while wait!" if $dead == -1 ; print "Subnet $childpid{$dead} scan completed by child $dead\n" ; delete $childpid{$dead} ; } while (keys(%childpid) > 0) ;

In reply to Run N similar tasks in parallel by bronto

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



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-23 14:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found