Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: Net::SSH2 woes

by syphilis (Chancellor)
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.


Replies are listed 'Best First'.
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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://979553]
[Discipulus]: indeed marto I dunno. I tested once and many years ago. But I'm not the programmer yardstick. I try to install Padre via cpan now just to add a line about it in the issue. Strawberryperl all life long!
[marto]: yeah, when I worked on Windows I did use Np++ all the time

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (11)
As of 2018-06-25 08:56 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.