Hi all, Hi i'm able to connect to mulitple hosts now and it will login to all the hosts specified in the input file (input.txt)
if there is a host which doesn't exist in input file (input.txt), the program ends without going to the next host in the file for example: if the input.txt has three hosts ( host1, host2 and host3), assuming that host2 doesn't exist.....then it will not login to host2 and execute the command and give the output. i also tried warn instead of die.............can somebody help me with this, i have pasted my code below #!/usr/bin/perl -w use strict; use warnings; use Net::SSH2; use constant BUFLEN => 10_000; open my $input, '<', 'input.txt' or die $!; open OUTPUT, '>', 'output.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 OUTPUT "$buf\n"; print OUTPUT "\n\n\n"; print OUTPUT "----------------------------------------------------------------------------------------------------------------"; $chan->exec('exit'); $ssh2->disconnect; Thanks, Monnappa On Thu, Feb 26, 2009 at 11:19 PM, Rob Dixon <rob.di...@gmx.com> wrote: > 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; > } >