adambot has asked for the wisdom of the Perl Monks concerning the following question:
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; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Piping and Parallel Forks
by Anonymous Monk on Jan 05, 2012 at 01:29 UTC | |
|
Re: Piping and Parallel Forks
by wwe (Friar) on Jan 05, 2012 at 10:32 UTC | |
by adambot (Acolyte) on Jan 05, 2012 at 15:58 UTC | |
by afoken (Chancellor) on Jan 05, 2012 at 17:05 UTC |