monnappa appaiah wrote:
> 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
It is better to use the 'warnings' pragma than the command-line switch.
use warnings;
> use strict;
> use Net::SSH2;
>
> open INPUT, "< input.txt"
It is preferred to use the three-parameter form of 'open', together with lexical
file handles. You should also check the status of the open to make sure it
succeeded.
open my $input, '<', 'input.txt' or die $!;
>
> while (<INPUT>) {
The reason your program isn't working is probably because you have left a
trailing record separator on the end of each line read. Remove it before using
it.
while (<$input>) {
chomp;
> my $ssh2 = Net::SSH2->new();
> $ssh2->connect("$_") or die "Unable to connect host $@ \n";
It is wrong to quote Perl variables as it does something very specific that you
probably don't want.
> $ssh2->auth_password('<username>','<password>');
> my $chan = $ssh2->channel();
> $chan->exec('ls -al');
> my $buflen = 10000;
It is better to set this value as a constant near the beginning of the program
use constant BUFLEN => 10_000;
> my $buf1 = '0' x $buflen;
There is no need to pre-extend the scalar variable. This isn't C, and Perl will
do the right thing for you. More accurately the Net::SSH2 module will. And why
is it buf1 when there is no buf2 etc.?
my $buf;
> $chan->read($buf1, $buflen);
You cannot assume that you have read all of the data to be returned, so you
should check it.
my $read = $chan->read($buf, BUFLEN);
> print "BUF1:\n", $buf1,"\n";
There is no need for multiple parameters to print.
print "BUF1:\n$buf\n";
> $chan->exec('exit');
> $ssh2->disconnect();
> }
The changes I have noted result in a program that looks something like this. The
program is untested except to make sure that it compiles.
HTH,
Rob
use strict;
use warnings;
use Net::SSH2;
use constant BUFLEN => 10_000;
open my $input, '<', 'input.txt' or die $!;
while (<$input>) {
chomp;
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 $buf;
my $read = $chan->read($buf, BUFLEN);
die 'More than ', BUFLEN, ' characters in listing' if $read >= BUFLEN;
print "BUF:$buf\n";
$chan->exec('exit');
$ssh2->disconnect;
}
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/