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

I am trying to use Net::SSH:Expect in a script that has threading on a linux host (2.6.34.9-69.fc13.x86_64 #1 SMP Tue May 3 09:23:03 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux). When my script starts, it opens a thread and a queue for logging.

Then I try to open a SSH session (using RSA). It always fails on read_all with SSH_CONNECTION_ABORTED.

When I omit the logging thread, the ssh connection works. Is there a known conflict between Net::SSH::Expect and threads?

First I set up the queue and the LogMonitor thread:
sub _startMonitorThread { $self = shift; $self->{LogQ} = Thread::Queue->new; my $id = "LogMonitor"; my $thrd = new threads \&_LogMonitor, $id; if ( !$thrd ) { $self->{errormsg} = "$thisFunc:Cannot start $id thread"; $self->TestLogError(); return -1; } $self->{thrds}->{logmon} = $thrd; $self->{logqueue} = 1; return 0; }
Then I create an ssh connection:
sub getsshrsa { my $thisFunc = ( caller(0) )[3]; my $opts = shift; my $ip = $opts->{ip}; $opts->{timeout} = 5 if (!$opts->{timeout}); my $ssh = Net::SSH::Expect->new ( host => $ip, user => $opts->{user}, password => $opts->{password}, ssh_option => "-T", raw_pty => 1 ); my $error = undef; $ssh->run_ssh() or $error = "SSH process couldn't start: $!"; return $error if ($error); $ssh->send(" "); sleep 2; my $output = $ssh->read_all(5); # <-- fails here return $ssh; }
The result is this:
Killed by signal 1. SSHConnectionAborted at MGComm.pm line 87 at /opt/ActivePerl-5.14/site/lib/Net/SSH/Expect.pm line 585 Net::SSH::Expect::_sec_expect('Net::SSH::Expect=HASH(0x34f9890 +)', 5, '-re', 'Regexp=REGEXP(0x351a2c0)') called at /opt/ActivePerl-5 +.14/site/lib/Net/SSH/Expect.pm line 419 Net::SSH::Expect::read_all('Net::SSH::Expect=HASH(0x34f9890)', + 5) called at MGComm.pm line 87 MGComm::getsshrsa('HASH(0x35235e0)') called at MGcld.pm line 8 +7

Replies are listed 'Best First'.
Re: Net::SSH:Expect and threads incompatible?
by zentara (Cardinal) on Dec 17, 2012 at 22:24 UTC
    What does googling for Net::SSH::Expect threadsafe tell you? Its been asked many times before.

    I'm not really a human, but I play one on earth.
    Old Perl Programmer Haiku ................... flash japh
      Thanks. I didn't think it would be an issue, when I'm only creating the ssh connection in the main thread. But obviously it is.