On 2/26/09 Thu Feb 26, 2009 8:26 AM, "monnappa appaiah"
<[email protected]> scribbled:
> Hi all,
>
>
> I'm using windows machine and i'm using Net::SSH2 module to connect
> to remote machine
> 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();
> }
Perl will include the line terminator in the return from the input operator
(<>). You need to remove the terminator characters from the input before
using it as a host name. Also, you can use an explicit variable name and
indent loops for better code readability, and you don't need to quote
variables, suggesting the following:
while( my $host = <INPUT> ) {
chomp($host);
my $ssh2 = Net::SSH2->new();
$ssh2->connect($host) or die "Unable to connect host $host";
...
Note: the documentation on Net::SSH2 doesn't state if errors will be
returned in $@ or $!, the normal variables, so I haven't included either in
the die statement. Perhaps you or somebody else knows better (I cannot
install Net::SSH2 on my system).
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/