Code
- 1. #usr/bin/perl
use strict ;
use warnings ;
use diagnostics ;
use NET::SSH2 ;
print "User = " ;
my $user = <STDIN> ;
chomp $user ;
print "Password = " ;
my $pwd = <STDIN> ;
chomp $pwd ;
open (Hosts , 'ip.txt') or die ("Couldn't open file ip.") ;
open (Cmds , 'cmds.txt') or die ("Couldn't open cmds.") ;
my @ips = <Hosts> ;
my @cmds = <Cmds> ;
my $ip ;
my $cmd ;
foreach $ip (@ips){
my $ssh = Net::SSH2->new();
chomp $ip ;
print "$ipn" ;
if (!$ssh->connect($ip)){
print "connection failedn" ;
exit (1) ;
}
if (!$ssh->auth_password($user,$pwd)){
print "Authentication failedn" ;
exit(1) ;
}
my $channel = $ssh->channel();
foreach $cmd (@cmds){
print "$cmdn" ;
my $command = $channel->exec($cmd) or die $cmd;
my $output;
my $len = $channel->read($output,4096) ;
# print "$cmdn" ;
print "output isn $outputn##########################n";
}
}