Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
I want to have something like this but without forkmanager, just with forking in perl.. I have an example of the EXACT type of forking i want below, but i cant seem to figure out how to use it for my own purposes. *note im new to perl Heres the code:use Parallel::ForkManager qw( ); my $pm = Parallel::ForkManager->new(int(@files/4)); for my $file (@files) { my $pid = $pm->start and next; ... do something with $file ... $pm->finish; # Terminates the child process }
#!/usr/bin/perl use Net::IP; use LWP::UserAgent; use vars qw( $PROG ); ( $PROG = $0 ) =~ s/^.*[\/\\]//; #Usage if ( @ARGV == 0 ) { print "Usage: ./$PROG [START-IP] [END-IP] [THREADS] [TIMEOUT] +[OUTPUT]\n"; exit; } my $threads = $ARGV[2]; my @ip_team = (); $|= 1; my $ip = new Net::IP ("$ARGV[0] - $ARGV[1]") or die "Invaild IP Rang +e.". Net::IP::Error() ."\n"; print "[!]Starting with $threads threads\n[!]Scanning $ARGV[0] to $ARG +V[1]\n"; while ($ip) { push @ip_team, $ip++ ->ip(); if ( $threads == @ip_team ) { Scan(@ip_team); @ip_team = () } } Scan(@ip_team); sub Scan { my @Pids; foreach my $ip (@_) { my $pid = fork(); die "Could not fork! $!\n" unless defined $pid; if (0 == $pid) { my $ua = LWP::UserAgent->new; $ua->timeout($ARGV[3]); my $response = $ua->get("http://$ip"); if ($response->is_success) { print "Found one $ip!\n"; open (MYFILE, ">>$ARGV[4]"); print MYFILE "$ip\n"; close (MYFILE); } else { die "[-] No Webserver Found!"; } exit } else { push @Pids, $pid } } foreach my $pid (@Pids) { waitpid($pid, 0) } }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Forking list in perl without Parallels ForkManager
by hippo (Archbishop) on Apr 05, 2014 at 09:33 UTC | |
|
Re: Forking list in perl without Parallels ForkManager
by zentara (Cardinal) on Apr 05, 2014 at 11:20 UTC |