Update: Thanks for the help! I'll try the eval ( ) block in the morning.
I'm trying to write a script that will iterate over a list of IP addresses, connecting to each via SSH and executing some commands. It's possible that one or more of the IP addresses could be invalid, in which case I want my script to log that fact and move on to the next address. Instead, the script just halts.
I've tried both Net::SSH::Expect and Net::SSH::Perl.
Net::SSH::Expect code:
#!/usr/bin/perl require Net::SSH; require Net::SSH::Expect; my %hosts = ( "192.168.1.31" => "admin,password", "192.168.1.61" => "admin,password", ); my $ssh; open (LOG,">>./audit.log"); foreach $host (keys %hosts) { myAuditLog(LOG,"Accessing $host . . ."); ($login,$passwd) = split /,/,$hosts{$host}; $ssh = Net::SSH::Expect->new ( host => "$host", user => "$login", password => "$passwd", raw_pty => 1 ); $loginOutput = $ssh->login() || myAuditLog(LOG,"Login has failed. $! +"); # do stuff $ssh->close(); } close(LOG);
Net::SSH::Perl code: Same as above, except replace the Net::SSH::Expect->new and $ssh->login bit with:
$ssh = Net::SSH::Perl->new($host); $ssh->login($login,$passwd) || myAuditLog(LOG,"Login has failed. $!");
(And, obviously, require Net::SSH::Perl.)
In both cases, as soon as $ssh->login is evaluated for an invalid IP address, the script prints an error message to STDOUT. For Net::SSH::Expect the error is:
SSHAuthenticationError Login timed out. The input stream currently has the contents bellow: at /usr/local/lib/perl5/site_perl/5.10.0/Expect.pm line 828Then the script just . . . stops. myAuditLog is not evaluated, so I can't get at $! or $loginOutput. (myAuditLog just prints log messages and timestamps to LOG. I've tried printing to STDOUT instead of calling myAuditLog, but the print command doesn't get evaluated either.)
Help! :(
BTW, I do have root access to the machines on which this script will run, so if there's a different module I should be using instead of Net::SSH::Expect or Net::SSH::Perl, feel free to suggest it.
Thanks in advance!
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |