in reply to Re^6: Problem with Net::SSH::Expect on unavailable hosts
in thread Problem with Net::SSH::Expect on unavailable hosts

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.

Replies are listed 'Best First'.
Re^8: Problem with Net::SSH::Expect on unavailable hosts
by sierpinski (Chaplain) on Jul 02, 2009 at 16:22 UTC
    Thanks a ton, that did the trick. I turned my 'manual debugging' back on, and saw that it skipped the offending host immediately and went on to the next one.

    As for cleaning up, you are right on both counts, I had a lot of print statements in there trying to discern where the problems were, tons of lines commented out, etc. I also plan to go back and capture all the return values, but was going to wait until I got the error resolved.

    The only thing now that's left is to have it notify me that a server is unavailable, and that's the easy part... thanks so much for your help!!

    -Tim