in reply to Parallel::ForkManager Problem
So there I've replaced the push with a simple write to a file and once the children are finished the parent process simply reads the file. See. flock and seek for more info on the functions used and perlipc for info on communication between processes.use strict; use warnings; use Fcntl ':flock'; use Parallel::ForkManager; my $PORT_NO = '80'; # Default port of HTTP my $SERVICE = "http"; my $pm = new Parallel::ForkManager(20); # At any instant maximum 20 pr +ocesses can run simultaneously my $ip; # Stores the IP address supplied by user if(!$ARGV[0]) { print "Program intended to check the machines where webserver is run +ning. \n"; print "USAGE: perl wst.pl [ip(xxx.xxx.xxx)] \nInput only 3 octets\n" +; exit; } if($ARGV[0] =~ /^\d{1,3}\.\d{1,3}\.\d{1,3}$/) { $ip = $ARGV[0]; } else { print "Enter IP in valid format (xxx.xxx.xxx) \n"; print "USAGE: perl wst.pl [ip(xxx.xxx.xxx)] \n"; exit; } ## file to write to open(my $fh, '+>', "ipaddresses.txt") or die "ack: $!"; foreach my $i (1..254) { my $ip_add = sprintf("%s.%s",$ip,$i); my $pid = $pm->start and next; my $val = `nmap -sT -p $PORT_NO $ip_add | grep $SERVICE`; if ($val) { my $test = (split(/\s+/,$val))[1]; if ($test =~ m/open/) { flock $fh, LOCK_EX; print "$ip_add\n"; flock $fh, LOCK_UN; } } $pm->finish; } print "reaping....might take some time\n"; $pm->wait_all_children; seek $fh, 0, 0; print "Web Server is running at : ", <$fh>, "\n"; exit 0;
_________
broquaint
|
|---|