Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

Forking and Net::SSH::Perl

by eff_i_g (Curate)
on Apr 01, 2011 at 20:25 UTC ( #896986=perlquestion: print w/replies, xml ) Need Help??
eff_i_g has asked for the wisdom of the Perl Monks concerning the following question:


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?

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; }
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/ line 175 Bad packet length 629981479 at /usr/local/lib/perl5/site_perl/5.10.0/N +et/SSH/Perl/ line 175 Trying 'ls -1'... ==================== Use of uninitialized value $out in concatenation (.) or string at ./tm line 53. Connection failed: Connection reset by peer at ./ line 52

Replies are listed 'Best First'.
Re: Forking and Net::SSH::Perl
by salva (Abbot) on Apr 05, 2011 at 08:29 UTC
    You can not share an Net::SSH::Perl connection between different processes. It just doesn't work.

    You have to open a new connection from every child. If that slows down your script too much, reimplement it using a workers+queue aproach.

    And BTW, you can get OpenSSH packed for Solaris from Sunfreeware. Net::OpenSSH does support sharing the SSH connection with child processes.

        Thread::Queue may be a good choice as sharing data between threads is easier than between processes but you will have to check that Net::SSH::Perl and its dependencies are thread safe.

        In any case, I am not an expert in that area, you will probably get better answers if you post a new question.

Re: Forking and Net::SSH::Perl
by eff_i_g (Curate) on Apr 04, 2011 at 18:30 UTC

    This may be the issue as I'm using SunSSH:

    Due to fork safety issues the application must re-login if the child continues to use the PKCS#11 engine.

    Whether or not I'm affected by this, my issue does go away if I reconnect in the child prior to running the command.

    Problem solved (for now).

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://896986]
Approved by Corion
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2017-09-22 14:09 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (264 votes). Check out past polls.