我的 perl 代码不允许超过 10 个分叉。对于下面的 perl 代码,每当我在读入脚本的机器列表中使用超过 10 台机器时,perl 脚本只会为 10 台机器分叉 10 个进程,其余的则因错误而死:
SSHProcessError ssh 进程已终止。在 serverLogin.pl 44 它死在它说 $ssh->waitfor('主机的真实性*',15); 的那一行。
PERL 脚本:
#!/usr/bin/perl -w
use Net::SSH::Expect;
use Term::ReadKey;
print "please enter filename:\n";
$filename = ReadLine;
chomp $filename;
print "please enter user ID:\n";
$userID = ReadLine;
chomp $userID;
print "please enter password:\n";
ReadMode 'noecho';
$passwordforuser = ReadLine 0;
chomp $passwordforuser;
ReadMode 'normal';
open READFILE,"<","$filename" or die "Could not open file listofmachines\n";
my @listofmachines = <READFILE>;
foreach $machine (@listofmachines)
{
my $pid=fork();
if ($pid){
push(@childprocs,$pid);
}
elsif ( $pid == 0 ) {
my $ssh = Net::SSH::Expect->new (
host => "$machine",
user => "$userID",
password=> "$passwordforuser",
timeout => 25,
raw_pty => 1,
);
my $login_output = $ssh->run_ssh or die "Could not launch SSH\n";
$ssh->waitfor('The authenticity of host*',15);
#print "This output for machine $machine\n";
$ssh->send("yes");
$ssh->waitfor('password: ', 15);
$ssh->send("$passwordforuser");
$ssh->waitfor('$ ', 10);
my @commresult=$ssh->exec("uptime");
print $login_output;
print @commresult;
exit 0;
}
else {
die "Could not Fork()\n";
}
}
foreach(@childprocs){
waitpid($_, 0)
}
请帮忙。谢谢,蓝蓝。