Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

Net::SSH2 woes

by vdubjunkie (Novice)
on Jul 02, 2012 at 21:33 UTC ( #979541=perlquestion: print w/ replies, xml ) Need Help??
vdubjunkie has asked for the wisdom of the Perl Monks concerning the following question:

It's hard to explain the elation I felt when I found a working ssh module recently. I've hit a low spot though, when I attempted to take my functioning routine and wrap it up inside a list of hosts.
#!/usr/bin/perl use strict; use Net::SSH2; my $hf = 'list.txt'; my $user = 'user'; my $pass = 'pass'; my $ssh = Net::SSH2->new(); $ssh->debug(1); open (L,"<$hf") || die "$!"; while(<L>) { chomp $_; print "Connecting now..\n"; $ssh->connect("$_") || warn "$!"; $ssh->auth_password("$user","$pass"); my $chan = $ssh->channel(); $chan->exec('dir'); $chan->close(); } close L;
This was a test script I used to simplify troubleshooting, and I still got the following result:
# ./test.pl Connecting now.. libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x841ed400 Net::SSH2::Channel::DESTROY Connecting now.. Resource temporarily unavailable at ./test.pl line 14, <L> line 2. libssh2_channel_open_ex(ss->session, pv_channel_type, len_channel_type +, window_size, packet_size, ((void *)0) , 0 ) -> 0x0 Can't call method "exec" on an undefined value at ./test.pl line 21, < +L> line 2. Net::SSH2::DESTROY object 0x8663b6c0
It's the "Resource is temporarily unavailable" that I'm seeing when I don't have the debug running. I even threw in a sleep to see if that might help. The first host connects perfectly, and I can run commands, etc. That second host (which by the way IS online) fails reliably. Any suggestions?!

Comment on Net::SSH2 woes
Select or Download Code
Re: Net::SSH2 woes
by syphilis (Canon) on Jul 02, 2012 at 23:37 UTC
    I think the problem is that you're using the same Net::SSH2 object ($ssh) to connect to different hosts, though it's not exactly clear to me that this *ought* to be a problem at all.
    Try the below rendition (untested). It creates a new Net::SSH2 object for each host.
    use strict; use Net::SSH2; my $hf = 'list.txt'; my $user = 'user'; my $pass = 'pass'; open (L,"<$hf") || die "$!"; while(<L>) { chomp $_; my $ssh = Net::SSH2->new(); $ssh->debug(1); print "Connecting now..\n"; $ssh->connect("$_") || warn "$!"; $ssh->auth_password("$user","$pass"); my $chan = $ssh->channel(); $chan->exec('dir'); $chan->close(); undef $ssh; } close L;
    Perhaps the undef $ssh; is unecessary.

    Cheers,
    Rob

      The SSH session is created in Net::SSH2->new(), not $ssh2->connect(). Each SSH session can be used for only one connection, therefore, to connect to a new host, a new SSH session is required. So, you are correct: Net::SSH2->new() must be called for each host, inside the loop.

      You had it. I'm actually a bit embarrassed I didn't think of it myself, but I am a bit rusty. I can swear I pulled this format from an example I found some place. Don't people test their examples?! Thanks guys. perlmonks has always been the only place I need for PERL help!
Re: Net::SSH2 woes
by salva (Abbot) on Jul 03, 2012 at 05:31 UTC
    The first thing you should do is to ensure you have the latest version of Net::SSH2 compiled against the latest version of libssh2.

    Try also Net::OpenSSH.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://979541]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2014-12-25 11:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (160 votes), past polls