Hi, I have written a script to query the status of webserver at port 80. I have used nmap for that. as i have to query all the addresses of subnet, i used Parallel::ForkManager to run processes in parallel. But it si giving me some strange results. I m using a global array to strore the results of each IP address . But in the loop, the array gets re-initialized each time. Below is the code for it. Its running perfectly fine if i use a file to write the results instead of an array. Here is the code for it...
#! /usr/bin/perl
use strict;
use warnings;
use Parallel::ForkManager;
my @arr;
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 suppli
+ed by user
if (!$ARGV[0]) {
print "Program intended to check the machines where webserver is r
+unning. \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;
}
foreach my $i (1..254) {
my $ip_add = sprintf("%s.%s",$ip,$i);
#print $ip_add;
my $pid = $pm->start and next;
my $val = `nmap -sT -p $PORT_NO $ip_add | grep $SERVICE`;
#print $val;
if ($val) {
my $test = (split(/\s+/,$val))[1];
if ($test =~ m/open/) {
#print "Web Server at $ip_add is running.\n";
push (@arr,$ip_add);
#print "@arr\n";
}
}
$pm->finish;
}
print "reaping....might take some time\n";
$pm->wait_all_children;
print "Web Server is running at : @arr \n";
exit 0;
Can u all suggest any solution to it. Is this some kind of synchronization problem. Any help would be greatly appreciated.
Thanks
Neeraj
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.