Hi all,

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;
}

Reply via email to