Beefy Boxes and Bandwidth Generously Provided by pair Networks Frank
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Piping and Parallel Forks

by adambot (Initiate)
on Jan 04, 2012 at 19:51 UTC ( #946279=perlquestion: print w/ replies, xml ) Need Help??
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; }

Comment on Piping and Parallel Forks
Download Code
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
    Hi adambot maybe your code is better and more portable written using Net::DNS and Net::Ping? The first one lets does nslookup from inside perl code and the second one checks for connection to hosts (using icmp, udp and tcp "pings").

      I didn't realize that those even existed. I'll look into those ASAP. Thanks for the tips on using the modules and for the link on multithreading net::Ping. I should be able to take it from here.

      is there a nmap module? (i didn't see one in core, and i am still learning where to look otherwise)
        is there a [...] module? (i didn't see one in core, and i am still learning where to look otherwise)

        To find modules, http://search.cpan.org, the search page of the Comprehensive Perl Archive Network, is sufficient most of the times. To install modules, running cpan Name::Of::The::Module from a terminal window / command prompt is sufficient most of the times -- unless you use ActivePerl, where things are a little bit different.

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://946279]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (8)
As of 2014-04-19 16:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls