#!/usr/bin/perl
use strict;
use warnings;
use Net::SSH::Perl;
my ($ssh, $cmd, $x, $stdout, $stderr, $exit);
# commands to be passed to remote server
my @commands = (
'uptime',
'tail /var/log/secure',
'tail /var/log/messages',
'exit');
my $user = 'root';
my $pass = 'root1';
open LOGFILE, "> hw-survey-log" || die "could not open file $!";
# connect to many hosts, on after another
for ($x=3; $x<255; $x+=2){
# make new connection
$ssh = Net::SSH::Perl->new("10.0.0.$x",
protocol => 2) || next;
$ssh->login($user, $pass) || next;
print LOGFILE "\nHostname: 10.0.0.$x:\n";
# execute all commands
foreach $cmd (@commands){
# pass command and record results
($stdout, $stderr, $exit) = ssh->cmd($cmd);
print LOGFILE $stdout.$stderr.$exit;
}
}
close LOGFILE;
####
Can't connect to 10.0.0.3, port 22: No route to host at /usr/lib/perl5/vendor_perl/5.8.5/Net/SSH/Perl.pm line 206.
####
connect($sock, sockaddr_in($rport, $raddr))
or die "Can't connect to $ssh->{host}, port $rport: $!";