Ok, after looking into Expect a bit more, I think we've made this a bit more complicated than it needs to be. The read_all() method of Net::SSH::Expect calls the get_expect() in the same package. Per the doc, the get_expect() method will
die if the Expect object does not have a valid ssh connection. So...Just wrap your call to read_all() in an eval to catch the die and you should be good. You're original code, modified in this way:
my $ssh = Net::SSH::Expect->new (
host => "$current_server",
user => "$user",
raw_pty => 1
);
# test the login
eval {
$login_output = $ssh->run_ssh();
};
# I've tried to capture $@ or $! here but it never gives me an
+ything useful
my $rc = eval{($ssh->read_all(2) =~ />\s*|$\s*\z/) or die "whe
+re's the remote prompt?";};
unless($rc =~ /$your_expected_output_match_pattern/){
&MOVE_ON_TO_THE_NEXT_HOST;
}
$ssh->exec("stty raw -echo");
There are some other things that you should probably clean up in this example, like the poorly named $login_output and not checking the return value of the first eval (or $login_output for that matter). I'll assume you either plan to do that or were just a bit too aggressive in summarizing your code for this example.
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.