Short description: I am running a script on some HostA and this script resets HostB which I can control via ssh. To resume my interaction s with HostB I wanted to know when is HostB back online again. I followed the advice from quester and in a loop I try to connect to port 22 and read whatever the server listening there is about to say to me. Under normal circumstances, that server is the sshd and I should be reading a line beginning with "SSH". If this does not happen I go to sleep for 3 seconds and try again. I do this until I get my "SSH" line and then I go on with the rest of the script.
The thing is, the reboot procedure does take a while and in my try_to_connect-sleep loop I increment a counter just to see how many times I tried and just get some feedback that my script is still alive and probing the other host. the loop looks like this:
My curiosity was triggered by how the "trying again" messages are printed. First one comes as expected, then then second one comes after 2-4 seconds. The third one can take 10 or more seconds to show up and then they come at 3 sec intervals. I am not bothered by how long it takes to get access to sshd so the fact that the total is around 30-40 seconds is not an issue. I am just curious about this weird timing and why is it always the second iteration?do { $connected = connect(SOCK, $paddr); if($connected) { $line = <SOCK>; print("$line online\n"); close(SOCK); $connected = 0; if ($line =~ /SSH/) {# connected to sshd - I'm ready $done = 1; } else {# connected to ... who the heck is listening on port 22? ...some code here } } else { $attempts++; print("Trying again ... ($attempts) \n"); sleep(3); #sleep 3 seconds before trying again } } while(!$done);
Any ideas anyone?
Thanks!
In reply to Socket connect curiosity by RaduH
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |