Hey Monks!
I am trying to figure out how to combine the following code so that I do not have to call another script. Currently, the following code works. For 4,000 servers it will spawn 25 child processes at a time. Each child process will log into the server assigned, then get the uname information. However, if I try to combine the code I get the following error: "Cannot start another process while you are in the child process..."
It's not that big a deal if I have to do it with two different scripts, but was just trying to figure out if there was a way to do it all in one. The first two code blocks are the ones that work, and the third code block is the non-working attempt at combining.
#!/usr/bin/perl
use strict;
use FindBin qw ($Bin);
use lib "$Bin/lib";
use ForkManager;
use General;
use Benchmark;
getPassword("Please enter your password: ");
my $start = new Benchmark;
open SERVERS,"$ARGV[0]" or die $!;
my (@hosts)=<SERVERS>;
close SERVERS;
my $sshscript = "checkUname.pl";
my $maxProbes = 20; # set this to one to do one at a time
my $pm = new ForkManager($maxProbes);
foreach my $hostname (@hosts) {
$pm->start and next;
my $stdout = `./$sshscript $hostname`;
print "$stdout\n";
$pm->finish;
}
$pm->wait_all_children;
print "done\n";
my $end = new Benchmark;
my $diff = timediff($end, $start);
print "Time taken was ", timestr($diff, 'all'), " seconds";
#!/usr/bin/perl
use strict;
use FindBin qw ($Bin);
use lib "$Bin/lib";
use General;
use Functions;
my $host=$ARGV[0];
$General::password=$ENV{'PASSWORD'};
my $login = getPrompt($host);
die "Could not connect to $host" if $login ne "SUCCESSFUL!";
my $result = getOSType();
print $result;
#!/usr/bin/perl
use strict;
use FindBin qw ($Bin);
use lib "$Bin/lib";
use ForkManager;
use General;
use Benchmark;
getPassword("Please enter your password: ");
my $start = new Benchmark;
open SERVERS,"$ARGV[0]" or die $!;
my (@hosts)=<SERVERS>;
close SERVERS;
my $sshscript = "checkUname.pl";
my $maxProbes = 20; # set this to one to do one at a time
my $pm = new ForkManager($maxProbes);
foreach my $hostname (@hosts) {
$pm->start and next;
#my $stdout = `./$sshscript $hostname`;
my $login = getPrompt($hostname);
next if $login ne "SUCCESSFUL!";
my $stdout = getOSType();
print "$stdout\n";
$pm->finish;
}
$pm->wait_all_children;
print "done\n";
my $end = new Benchmark;
my $diff = timediff($end, $start);
print "Time taken was ", timestr($diff, 'all'), " seconds";
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.