I am having problems forking. I have a loop that keeps calling a subroutine which has the forking code. For some reason it will work for the first couple loops and Perl goes to hell saying that things have not been inititalized... I tried using different Perl modules like thread and Parallel::ForkManager, but the same thing always happens. Here is my current code:
#!/usr/bin/perl -w use strict; use IO::Socket; my $threads; my $pm; my $class; my $octect1 = 0; my $octect2 = 0; my $octect3 = 0; my $octect4 = 0; my $b; my $c; my $d; my $host; my $pid; my @pids; my $npids=0; my $wait_ret; if (@ARGV == 0){ print "Test Subnet Scanner\n Written by: Sapient2003\n\n"; die "Usage: $0 subnet threads\n Class A: $0 66 100\n Class B: $0 66.4 +7 100\n Class C: $0 66.47.159 100\n"; } ($octect1, $octect2, $octect3, $octect4) = split '\.', $ARGV[0]; $threads = $ARGV[1]; $class = ($ARGV[0] =~ tr/.//); if ($class == 0) { print "Processing Class A Scan"; for($b = 0; $b<255; $b++){ for($c = 0; $c<255; $c++){ for($d = 0; $d<255; $d++){ $host = "$octect1.$b.$c.$d"; } } } } elsif ($class == 1) { print "Processing Class B Scan\n"; for($c = 0; $c<255; $c++){ for($d = 0; $d<255; $d++){ $host = "$octect1.$octect2.$c.$d"; } } } elsif ($class == 2) { print "Processing Class C Scan\n"; for($d = 0; $d<255; $d++){ $host = "$octect1.$octect2.$octect3.$d"; checkhost($host); } } elsif ($class == 3) { print "Processing Single Host Scan\n"; $host = "$octect1.$octect2.$octect3.$octect4"; } else {die "Invalid class.\n";} sub checkhost{ $pid = fork(); if($pid > 0){ $npids++; if($npids >= $threads){ for(1..($threads)){ $wait_ret = wait(); if($wait_ret > 0){ $npids--; } } } next; }elsif(undef $pid){ print "Forking error\n"; exit(0); }else{ print "."; my $host = shift; my $sock = new IO::Socket::INET( PeerAddr => $host, PeerPort => 80, Proto => 'tcp', Timeout => 3); if ($sock){ print $sock "GET / HTTP/1.0\n\n"; while (<$sock>) { if (/^Server:\s*(.*)/) { print "\nFound: $host\n"; } close($sock); } } } } print "Done forking, $npids children remain.\n"; for(1..$npids){ my $wt = wait(); if($wt == -1){ print "Hey $!\n"; redo; } } print "Done!\n";

In reply to Forking problems by Sapient2003

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



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.