Greetings!

I am trying to run a for loop that forks and runs a subroutine and then capture the output from the sub into a hash so that i can sort the output of the sub, but i can't figure out the piping.

Originally i was writing everything to a file, then using the packed default sort, however, i was hoping to be able to do all of this in memory.

Here is the code (without the piping) - any other coding tips are welcome as well since i am a bit green:
#!/usr/bin/perl use strict; use warnings; my @childs; my $net = shift(); my $netdns = ""; my $addns = ""; for ( my $count = 1; $count <= 254; $count++) { my $ip; $ip = $net . "." . $count; my $pid = fork(); if ($pid) { # parent push(@childs, $pid); } elsif ($pid == 0) { # child &check($ip); exit 0; } else { die "couldnt fork: $!\n"; } } foreach (@childs) { my $tmp = waitpid($_, 0); } sub check { my $address = shift; my ($undef, $ad, $networks, $ping, $ssh, $rdp); open(ADDNS, "nslookup $address $addns|") || return 1; local $/ = "\n\n\n"; while (<ADDNS>) { ($undef,$undef,$undef,$undef,$undef,$undef,$undef,$ad) = s +plit(/[\t \n]+/); if (defined $ad && length $ad > 0) { if ( $ad !~ m/edu/ ) { $ad = "none"; } } else { $ad = "none"; } } close ADDNS; open(NETDNS, "nslookup $address $netdns|") || return 1; local $/ = "\n\n\n"; while (<NETDNS>) { ($undef,$undef,$undef,$undef,$undef,$undef,$undef,$network +s) = split(/[\t \n]+/); if (defined $networks && length $networks > 0) { if ( $networks !~ m/edu/ ) { $networks = "none"; } if ( $networks =~ m/[\d]{5,}/ ) { $networks = "none"; } } else { $networks = "none"; } } close NETDNS; open(PING, "ping -c 1 -w 1 $address|grep received|") || return 1; while (<PING>) { ($undef,$undef,$undef,$undef,$undef,$ping) = split(/[\t \n +]+/); if ( $ping =~ m/100%/ ) { $ping = "N"; } if ( $ping =~ m/0%/ ) { $ping = "Y"; } } close PING; if ( $ping =~ "Y" ) { open(NMAP, "sudo nmap -sS -p 22,3389 $address |grep tcp|") || +return 1; local $/ = "\n\n\n"; while (<NMAP>) { ($undef,$ssh,$undef,$undef,$rdp,$undef) = split(/[\t \ +n]+/); if ( $ssh =~ m/open/ ) { $ssh = "Y"; } else { $ssh = ""; } if ( $rdp =~ m/open/ ) { $rdp = "Y"; } else { $rdp = ""; } } close NMAP; } else { $ssh = ""; $rdp = ""; } printf "%-18s %-4s %-4s %-4s %-35s %-35s\n", $address, $ping, $rdp, $s +sh, $ad, $networks; exit; }

In reply to Piping and Parallel Forks by adambot

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.