Monks,
Net::SSH::Perl works fine for me; however, when I add Parallel::ForkManager to the mix I start seeing the gems "Bad packet length" and "Connection failed: Connection reset by peer."
In the example below the connections are fine, the SSH commands are fine, but the SSH commands within the fork go amiss. This post says "that's just the way it is," yet Net::OpenSSH across fork & exec says otherwise (but it's OpenSSH, which I don't have).
This is beyond my realm and my Googling is chafing. What can I do here?
Many thanks.
P.S. Being new to the SSH+Perl world, do you agree with Why should you use Net::OpenSSH instead of any of the other Perl SSH clients available? And what's recommended when lacking OpenSSH?
Code:
use warnings;
use strict;
use Net::SSH::Perl;
use Parallel::ForkManager;
### Add your own.
my $p_host = '';
my $d_host = '';
my $ssh_key_file = '';
### For command execution.
my @cmds = (
'ls',
'ls -l',
'ls -1',
);
my ($out, $exit);
### Establish two connections. Works fine.
my $p_ssh = Net::SSH::Perl->new(
$p_host,
identity_files => [$ssh_key_file],
);
$p_ssh->login && print "$p_host connected.\n";
my $d_ssh = Net::SSH::Perl->new(
$d_host,
identity_files => [$ssh_key_file],
);
$d_ssh->login && print "$d_host connected.\n";
### Try the commands on each host. Works fine.
for my $cmd (@cmds) {
print "Trying '$cmd'...\n";
print '=' x 20, "\n";
for my $server ($d_ssh, $p_ssh) {
($out, undef, $exit) = $server->cmd($cmd);
print "$out\n\n";
die if $exit;
}
}
### Now attempt the same thing in a fork. Bad news.
for my $cmd (@cmds) {
print "Trying '$cmd'...\n";
print '=' x 20, "\n";
my $pfm = new Parallel::ForkManager(2);
for my $host ($d_ssh, $p_ssh) {
my $pid = $pfm->start and next;
($out, undef, $exit) = $host->cmd($cmd);
print "$out\n\n";
die if $exit;
$pfm->finish;
}
$pfm->wait_all_children;
}
Output:
host1 connected.
host2 connected.
Trying 'ls'...
====================
...listing from host1...
...listing from host2...
Trying 'ls -l'...
====================
...listing from host1...
...listing from host2...
Trying 'ls -1'...
====================
...listing from host1...
...listing from host2...
Trying 'ls'...
====================
...listing from host1...
...listing from host2...
Trying 'ls -l'...
====================
Bad packet length 3032450590 at /usr/local/lib/perl5/site_perl/5.10.0/
+Net/SSH/Perl/Packet.pm line 175
Bad packet length 629981479 at /usr/local/lib/perl5/site_perl/5.10.0/N
+et/SSH/Perl/Packet.pm line 175
Trying 'ls -1'...
====================
Use of uninitialized value $out in concatenation (.) or string at ./tm
+p.pl line 53.
Connection failed: Connection reset by peer
at ./tmp.pl line 52