http://www.perlmonks.org?node_id=905187

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

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?