-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Hi there.
My biggest comment isn't the answer to your question, but I will
attempt to address that too.
I really think you're doing yourself a disservice by just throwing
your program commands on lines, not indenting according to best
practices. It makes your code unreadable, and can make it very hard
to debug the more involved your programs get. Consider something like
the following:
#!/usr/bin/perl -w
#######################
#
# Consider placing a block here describing your program, who wrote it
# the date it was written, it's usage and even a changelog.
#
# When you come back to it down the road to modify it
# (or to steal a routine from it) it'll be much easier to read
#
#######################
use strict;
use Net::SSH2;
# Consider commenting important structs:
# setup new connection in an OO sort of way:
my $ssh2 = Net::SSH2->new();
# connect to the host or die
$ssh2->connect("hostname") or die "Unable to connect host $@ \n";
# send auth
$ssh2->auth_password('<username>','<password>');
my $chan = $ssh2->channel();
# run an ls on the box
$chan->exec('ls -al');
# set a buffer to handle the output
my $buflen = 10000;
my $buf1 = '0' x $buflen;
$chan->read($buf1, $buflen);
# print the result
print "BUF1:\n", $buf1,"\n";
# exit the routine
$chan->exec('exit');
# Close the connection
$ssh2->disconnect();
Especially in an ide such as komodo or vim, syntax highlighting will
make this a joy to read and troubleshoot.
Consider picking up Damian's book:
http://oreilly.com/catalog/9780596001735/index.html
It'll serve you well and is a must have for the serious perl programmer.
Now... on to your question.
There's a few ways to feed in the list of hosts you want to use to hit
all the boxes in a host list and execute your command. Like most
things in Perl, TMTOWTDI! (There's more than one way to do it)
The simplest would be to have a file list out there that you've
referenced as a variable:
#!/usr/bin/perl -w
use Net::SSH2;
use strict;
# Set up variables to use
my $hostlist = "/path/to/hostlist.txt";
my @hosts = ();
# open the hostlist filehandle, and read in values
open (hostlist_fh, "<", $hostlist)
# fail gracefully if something happens
or die "Couldn't open $hostlist for reading: $!";
# while reading the list
while (<hostlist_fh>) {
# clean up the values
chomp;
# stuff them into an array
push(@hosts, $_);
}
# for each element of the array
foreach my $host (@hosts) {
# clean it up again
chomp($host);
# do the SSH stuff
my $ssh2....
{ all your code here. host variable is $host }
}
Also, you can put the connect routine in a subroutine and call it,
passing in the host like so:
foreach my $host (@hostlist){
ssh_routine();
}
sub ssh_routine{
<code here>, host is now shifted out or you can explicitly call him by
variable name $host
}
Just experiment with it and see which works most like you think, and
is easy to come back to and use.
Note: none of the above is tested. I just wrote it off the top of my
noggin.
- --jms
On Feb 26, 2009, at 11:26 AM, 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
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();
}
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.10 (Darwin)
iEYEARECAAYFAkmueCwACgkQRo+D37WpOWv2LgCggaZsSLMplB8tOaGqNOgWv4tW
rEwAoI5UFvDur8aeMX200qvV9qDmgVoF
=B3cJ
-----END PGP SIGNATURE-----
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/