For what it's worth, I went the eval route, and it works perfectly. I have a script that uses Net::SSH::Expect to connect to a list of servers one at a time, and running various commands and then parsing the output (looking for errors, failures, etc). I was running into issues when the ssh connection would die suddenly (because the server had been turned off) and I got around that by doing this:
my $rc = eval{( $ret =~ />\s*|$\s*\z/) or die "where's the remote prom
+pt?";};
if($rc) {
if($ret =~ m/[Pp]assword:/) {
print("Couldn't connect to host\n");
}
}
Originally I tried to check the return code (the $ret) or the return code of the run_ssh() that actually spawns the ssh process, but since the ssh spawned successfully (it just died soon after) it never returned any useful value, and the above command would break if the ssh object was already gone, or if the server was asking for a password (meaning my SSH keys were not set up on that host)
I can give credit to the Monks here for this solution, by the way!
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.