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

In reply to Forking and Net::SSH::Perl by eff_i_g

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.