问题描述
我有一个脚本,可以对存储在阵列中的服务器数量(@alive_hosts
)进行SSH,并在那些主机(这些主机是IP地址)上执行某些命令。
...
use Net::Ping;
use Net::OpenSSH;
use Parallel::ForkManager;
#these hosts are the IP addresses
my @hosts = ("host1","host2","host3","host4","host5");
my $p = Net::Ping->new();
foreach my $hostname (@hosts){
my ($ret,$duration,$ip) = $p->ping($hostname);
push (@alive_hosts,$hostname) if $ret;
}
my $ssh;
my $command = "perl -e "print \"Hello..\"";
my $pm = Parallel::ForkManager->new(5);
LOOP:
foreach my $n (@alive_hosts) {
my $pid = $pm->start and next LOOP;
#doing ssh to the host and running the command
#lets say here I am executing simple Perl command
$ssh = Connect($n,"user","passwd");
$ssh->capture($command);
$pm->finish;
}
$pm->wait_all_children;
undef $ssh;
sub Connect {
my ( $host,$user,$passwd ) = @_;
my $ssh = Net::OpenSSH->new($host,user=>$user,password=>$passwd);
$ssh->error and die "Couldn't establish SSH connection: " . $ssh->error;
return $ssh;
}
...
执行此脚本时,经过一定时间后我将遇到错误。
Warning: Permanently added 'host5' (RSA) to the list of kNown hosts.
Couldn't establish SSH connection: unable to establish master SSH connection: master process exited unexpectedly at script.pl ....
实际上$command
可以在host1到host4中运行。但是当需要host5时,它无法运行。
我可以ping host5并连接起来对我来说很好。但是通过脚本,它总是无法执行命令。
- 我尝试将
master_opts => [-o => "StrictHostKeyChecking=no",'-vvv']
放在$ssh
子例程的Connect()
对象中。 - 在捕获中,我添加了这样的超时时间-
$ssh->capture({timeout = 10},$command);
但是在两种情况下都没有成功。
ssh版本:OpenSSH_5.3p1,OpenSSL 1.0.1e-fips 11 Feb 2013
有什么想法为什么总是无法在最后一个主机中运行命令?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)