Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Net::SSH2 woes

by syphilis (Canon)
on Jul 02, 2012 at 23:37 UTC ( #979553=note: print w/ replies, xml ) Need Help??


in reply to Net::SSH2 woes

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


Comment on Re: Net::SSH2 woes
Select or Download Code
Re^2: Net::SSH2 woes
by ig (Vicar) on Jul 03, 2012 at 06:32 UTC

    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.

Re^2: Net::SSH2 woes
by vdubjunkie (Novice) on Jul 03, 2012 at 17:55 UTC
    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!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2014-09-20 08:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (157 votes), past polls