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 828

Then 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!


In reply to Net::SSH::Perl, Net::SSH::Expect crashes script if host is unreachable by LadyEngineer

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.