I'm using Net::OpenSSH to execute commands on a few remote servers. After the first command completes, I need to reboot the machine and reconnect after it comes back up, then execute more commands. Any advice on an efficient way to reconnect to the rebooted server via SSH as soon as it's back up? i.e. what's a good way to test and see when sshd is once again accepting connections? Here's what I have so far:

my %ssh; my @hosts = qw/ server1.example.com server2.example.com /; my @cmd = "list of shell commands ending with reboot"; for my $host (@hosts) { $ssh{$host} = Net::OpenSSH->new($host, master_opts => [-i => "/path/ +to/ssh_key"], async => 1); $ssh{$host}->error and die "SSH connection to $host failed: " . $ssh +{$host}->error; } for my $host (@hosts) { $ssh{$host}->system("@cmd"); }

This will get me to the point where all of the initial commands are run, and the servers are rebooted. From here, I can't think of a good way to quickly determine when SSHD is running so I can reconnect and continue with the post-reboot commands. I tried variations using $ssh{host}->test("some shell command"); but I'd like to come up with a better solution. Any ideas?


In reply to Net::OpenSSH fastest way to reconnect to a rebooted machine? by Anonymous Monk

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.