While SSH'ing following error occures - unable to establish master SSH connection: master process exited unexpectedly

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-28 07:39:58

问题


I have a script which does ssh to number of servers (@alive_hosts) stored in an array and executes certain commands on those hosts (these hosts are IP address).

...
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;
}
...

When I execute this script, I am getting below error after certain time.

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 ....

Actually $command is able to run in host1 to host4. But when it takes host5, its unable to run it. I could able to ping host5 and connecting looks fine to me. But through the script it always fails to execute the command.

  1. I have tried putting master_opts => [-o => "StrictHostKeyChecking=no", '-vvv'] in $ssh object in Connect() subroutine.
  2. In capture, I added timeout period like this - $ssh->capture({timeout = 10}, $command);
    But didn't succeeded in both the cases.

ssh version: OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013

Any idea why it's failing to run command in last host always?

来源:https://stackoverflow.com/questions/64450923/while-sshing-following-error-occures-unable-to-establish-master-ssh-connectio

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!