gurbo has asked for the wisdom of the Perl Monks concerning the following question:

I'm using Net::SSH::Perl and threads in a script and it hangs the second time I create a Net::SSH::Perl instance.
When it runs, the first thread goes ok, but the second thread stops after the line (in the debug output) containing "Remote protocol version" and before "Net::SSH::Perl Version...".
Also, the cpu usage goes up to 100%.

This looks the same as Problem with threads in Net::SSH:Perl

The script:

use Net::SSH::Perl; use threads; sub mydate { my $id = threads->tid(); my $ssh = new Net::SSH::Perl('127.0.0.1', protocol => '2', identit +y_files => ['fxfer.key'], debug => 1); $ssh->login('rob'); print "[$id] date\n"; my ($out, $err, $exit) = $ssh->cmd('date',"\n"); print "[$id] out: $out\n"; print "[$id] err: $err\n"; print "[$id] exit: $exit\n"; } for my $i (1..2) { print "*** #$i\n"; my $thr = new threads \&mydate; $thr->join(); print "\n"; }
The log:
*** #1 uqbar.arkham.com.ar: Reading configuration data /home/rob/.ssh/config uqbar.arkham.com.ar: Reading configuration data /etc/ssh_config uqbar.arkham.com.ar: Connecting to 127.0.0.1, port 22. uqbar.arkham.com.ar: Remote version string: SSH-2.0-OpenSSH_4.7p1 Debi +an-8ubuntu1.2 uqbar.arkham.com.ar: Remote protocol version 2.0, remote software vers +ion OpenSSH_4.7p1 Debian-8ubuntu1.2 uqbar.arkham.com.ar: Net::SSH::Perl Version 1.30, protocol version 2.0 +. uqbar.arkham.com.ar: No compat match: OpenSSH_4.7p1 Debian-8ubuntu1.2. uqbar.arkham.com.ar: Connection established. uqbar.arkham.com.ar: Sent key-exchange init (KEXINIT), wait response. uqbar.arkham.com.ar: Algorithms, c->s: 3des-cbc hmac-sha1 none uqbar.arkham.com.ar: Algorithms, s->c: 3des-cbc hmac-sha1 none uqbar.arkham.com.ar: Entering Diffie-Hellman Group 1 key exchange. uqbar.arkham.com.ar: Sent DH public key, waiting for reply. uqbar.arkham.com.ar: Received host key, type 'ssh-dss'. uqbar.arkham.com.ar: Host '127.0.0.1' is known and matches the host ke +y. uqbar.arkham.com.ar: Computing shared secret key. uqbar.arkham.com.ar: Verifying server signature. uqbar.arkham.com.ar: Waiting for NEWKEYS message. uqbar.arkham.com.ar: Enabling incoming encryption/MAC/compression. uqbar.arkham.com.ar: Send NEWKEYS, enable outgoing encryption/MAC/comp +ression. uqbar.arkham.com.ar: Sending request for user-authentication service. uqbar.arkham.com.ar: Service accepted: ssh-userauth. uqbar.arkham.com.ar: Trying empty user-authentication request. uqbar.arkham.com.ar: Authentication methods that can continue: publick +ey,password. uqbar.arkham.com.ar: Next method to try is publickey. uqbar.arkham.com.ar: Publickey: testing agent key '' uqbar.arkham.com.ar: Public key is accepted, signing data. uqbar.arkham.com.ar: Key fingerprint: 9e:3d:6b:65:f2:ca:89:bb:06:42:33 +:7e:d8:e4:75:af uqbar.arkham.com.ar: Login completed, opening dummy shell channel. uqbar.arkham.com.ar: channel 0: new [client-session] uqbar.arkham.com.ar: Requesting channel_open for channel 0. uqbar.arkham.com.ar: channel 0: open confirm rwindow 0 rmax 32768 uqbar.arkham.com.ar: Got channel open confirmation, requesting shell. uqbar.arkham.com.ar: Requesting service shell on channel 0. [1] date uqbar.arkham.com.ar: channel 1: new [client-session] uqbar.arkham.com.ar: Requesting channel_open for channel 1. uqbar.arkham.com.ar: Entering interactive session. uqbar.arkham.com.ar: Sending command: date uqbar.arkham.com.ar: Requesting service exec on channel 1. uqbar.arkham.com.ar: channel 1: send eof uqbar.arkham.com.ar: channel 1: open confirm rwindow 2097151 rmax 3276 +8 uqbar.arkham.com.ar: input_channel_request: rtype exit-status reply 0 uqbar.arkham.com.ar: channel 1: rcvd eof uqbar.arkham.com.ar: channel 1: output open -> drain uqbar.arkham.com.ar: channel 1: rcvd close uqbar.arkham.com.ar: channel 1: obuf empty uqbar.arkham.com.ar: channel 1: output drain -> closed uqbar.arkham.com.ar: channel 1: close_write uqbar.arkham.com.ar: channel 1: send close uqbar.arkham.com.ar: channel 1: full closed [1] out: Mon Nov 24 18:36:36 ARST 2008 [1] err: [1] exit: 0 *** #2 uqbar.arkham.com.ar: Reading configuration data /home/rob/.ssh/config uqbar.arkham.com.ar: Reading configuration data /etc/ssh_config uqbar.arkham.com.ar: Connecting to 127.0.0.1, port 22. uqbar.arkham.com.ar: Remote version string: SSH-2.0-OpenSSH_4.7p1 Debi +an-8ubuntu1.2 uqbar.arkham.com.ar: Remote protocol version 2.0, remote software vers +ion OpenSSH_4.7p1 Debian-8ubuntu1.2
Is there anything I can do to solve this problem?

Thanks in advance

Replies are listed 'Best First'.
Re: Problem with Net::SSH::Perl and threads
by McD (Chaplain) on Nov 24, 2008 at 22:23 UTC
    I've recently had good luck with Net::SSH::Perl using forked children, rather than threads. If it's early enough in your design, and you're on a flavor of unix, consider forking rather than threading. (I know threads are more popular than forks these days, but unix is actually quite good at forking.)

    I used socketpair() and IO::Select to manage IPC with the kids. Works great.
Re: Problem with Net::SSH::Perl and threads
by salva (Canon) on Nov 25, 2008 at 09:12 UTC
    If you only need to run a simple command in every host, instead of Net::SSH::Perl just use the ssh command installed on your system:
    sub mydate { my $out = `ssh $host -i fxfer.key date 2>&1`; print "[$id] out: $out\n"; print "[$id] exit: $?\n"; }
    And if you really need stderr separated from stdout, you will have to implement it at a lower level than the backquotes (I will show you how to do it if you want).

    Lately, I have been working in Net::OpenSSH that can do almost anything Net::SSH::Perl does (and much more!). It uses OpenSSH under the hood, instead of its own implementation of the Secure Shell protocol... though it is still very alpha and does not work on Windows.

    Using Net::SSH2 can be another good option.

Re: Problem with Net::SSH::Perl and threads
by zentara (Cardinal) on Nov 25, 2008 at 13:24 UTC