Hi All,

           I'm facing another problem:

Below is the script to login to multiple hosts and then execute the command
and give the ouput in text file


This script will login to all the hosts in input.txt(this file contains
around 137 hosts)


but when i run the script....its giving me output for only 5 hosts whereas
input file has 137 hosts


How can i make it to login to all the hosts in the input file and excute
commands? .......can somebody please help me with this?




find the 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 $!;
open STDERR, '>', 'error.txt' or die $!;
while (<$input>) {
chomp;
my $ssh2 = Net::SSH2->new;
eval {$ssh2->connect($_)};
if($@) {
   warn "Unable to connect host $_: $...@\n" and next;
    }

$ssh2->auth_password('<enter username>','<enter password>');
my $chan = $ssh2->channel;
$chan->exec('ls -al');

my $buf;
my $read = $chan->read($buf, BUFLEN);
warn '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 Tue, Mar 10, 2009 at 3:30 PM, Dermot <paik...@googlemail.com> wrote:

> 2009/3/9  <r...@i.frys.com>:
> > Jerald Sheets wrote:
> >>
> >> On Mar 8, 2009, at 1:29 PM, Ron Bergin wrote:
> >>
> >>> On Mar 4, 4:46 am, que...@gmail.com (Jerald Sheets) wrote:
> >>>>
> >>>> 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.
> >>>>
> >>> [snip]
> >>>
> >>>> 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.
> >>>>
> >>> I agree, however your code as shown below doesn't follow "Perl Best
> >>> Practices".
> >>>
> >>> You use improper indentation, and failed to use vertical whitespace
> >>> making it harder to read/follow.
> >>>
> >>>> #!/usr/bin/perl -w
> >>>>
> >>> It's better to use the warnings pragma, instead of the -w switch
> >>
> >>
> > [snip]
> >
> >> Whether fully complying with Damian's book or not, you cannot argue
> >> the difference in readability (my point of the messages) in comparison
> >> to what was offered.
> >
> > Actually, I can make that argument.  The readability of your code wasn't
> any
> > better than the OP's.  In fact yours was more confusing due to the
> improper
> > use of indentation.  Indentation is done at the block level and when
> breaking
> > up a long statement across multiple lines.  Your indentation made me
> think I
> > missed an initialization of a block which made me re-read that portion of
> code
> > each time you indented.
> >
> > [snip]
> >>
> >> However on the -w switch, here are Damian's words:
>
> I hope I am not hijacking this topic any more that it already has been
> :) but has what are people's thoughts on the perlcritic. I haven't
> used it myself.
> Dp.
>
> --
> To unsubscribe, e-mail: beginners-unsubscr...@perl.org
> For additional commands, e-mail: beginners-h...@perl.org
> http://learn.perl.org/
>
>
>

Reply via email to