Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Net::OpenSSH fastest way to reconnect to a rebooted machine?

by regan99 (Initiate)
on May 17, 2011 at 20:07 UTC ( #905353=note: print w/ replies, xml ) Need Help??


in reply to Re: Net::OpenSSH fastest way to reconnect to a rebooted machine?
in thread Net::OpenSSH fastest way to reconnect to a rebooted machine?

What you've described is exactly what I'm trying to figure out how to do. The question is how do I test that the connection has either succeeded or failed? I tried this, since the Net::OpenSSH documentation says that a failure returns undef or an empty list:

$ssh{$host} = Net::OpenSSH->new($host, master_opts => [-i => "/path/t +o/key"]); if(defined $ssh{$host}) { print STDOUT "SSHD is back up. Continuing...\n"; } else { print STDOUT "Still waiting for SSH. Retrying in 5 seconds...\n" +; sleep 5; }

This fails though. Apparently the "no route to host" message that comes back from the failed connection attempt is enough to define $ssh{$host}. What's a good way to test to see if the connection is successful?


Comment on Re^2: Net::OpenSSH fastest way to reconnect to a rebooted machine?
Download Code
Replies are listed 'Best First'.
Re^3: Net::OpenSSH fastest way to reconnect to a rebooted machine?
by salva (Abbot) on May 17, 2011 at 20:26 UTC
    Use the error method to see if some error happened.
    for my $host (...) { $ssh{$host} = Net::OpenSSH->new($host, ...); if($ssh{$host}->error) { print STDOUT "Still waiting for SSH. Retrying in 5 seconds...\ +n"; sleep 5; redo; } else { print STDOUT "SSHD is back up. Continuing...\n"; } }

    If you are starting several connections in parallel, you can also use the wait_for_master method.

      That worked! Thanks for the reply (and the great module).

Re^3: Net::OpenSSH fastest way to reconnect to a rebooted machine?
by Anonymous Monk on Nov 19, 2012 at 16:49 UTC
    Passing in parameters works for me, without having to loop & check the error condition:
    my $ssh = Net::OpenSSH->new( $username . "\@" . $host, key_path => $key_path, master_opts => [-o => "ConnectionAttempts +=30", -o => "ConnectTimeout=10"] );

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://905353]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (10)
As of 2015-07-30 09:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (270 votes), past polls