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 processes 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 running. \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;