develooper Front page | perl.beginners | Postings from February 2009

connecting to multiple hosts using Net::SSH2

Thread Next
From:
monnappa appaiah
Date:
February 26, 2009 08:26
Subject:
connecting to multiple hosts using Net::SSH2
Message ID:
33fa55730902260826u25146294q16be18cd456f815a@mail.gmail.com
Hi all,


        I'm using windows machine and i'm using Net::SSH2 module to connect
to remote machine

below is the code i'm using, its working fine if i'm connecting to one host

#!/usr/bin/perl -w
use strict;
use Net::SSH2;


my $ssh2 = Net::SSH2->new();
$ssh2->connect("hostname") or die "Unable to connect host $@ \n";
$ssh2->auth_password('<username>','<password>');
my $chan = $ssh2->channel();
$chan->exec('ls -al');
my $buflen = 10000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
print "BUF1:\n", $buf1,"\n";
$chan->exec('exit');
$ssh2->disconnect();



How can i use the same piece of code to read a list of hosts from a text
file and then connect to all the hosts in that text file.....I tried the
below code but its not working....can somebody please help me with this.





#!/usr/bin/perl -w
use strict;
use Net::SSH2;

open INPUT, "< input.txt"

while (<INPUT>) {
my $ssh2 = Net::SSH2->new();
$ssh2->connect("$_") or die "Unable to connect host $@ \n";
$ssh2->auth_password('<username>','<password>');
my $chan = $ssh2->channel();
$chan->exec('ls -al');
my $buflen = 10000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
print "BUF1:\n", $buf1,"\n";
$chan->exec('exit');
$ssh2->disconnect();
}

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About