Create connection to interrogate client for when sshd has launched.

Here use create_connection instead of netcat, but they work in the same way.

BUG=chromium-os:22104
TEST=Run Servod test, and successfully interrogate client after reboot.

Change-Id: I7ac7b90d0c389b74db9361fb8b1a20e0db9ac211
Reviewed-on: https://gerrit.chromium.org/gerrit/32014
Reviewed-by: Tom Wai-Hong Tam <waihong@chromium.org>
Reviewed-by: Todd Broch <tbroch@chromium.org>
Commit-Ready: Chun-Ting Chang <ctchang@chromium.org>
Tested-by: Chun-Ting Chang <ctchang@chromium.org>
diff --git a/server/cros/servo_test.py b/server/cros/servo_test.py
index acbe465..aad3780 100644
--- a/server/cros/servo_test.py
+++ b/server/cros/servo_test.py
@@ -5,6 +5,7 @@
 import logging
 import os
 import re
+import socket
 import subprocess
 import time
 import xmlrpclib
@@ -162,6 +163,21 @@
                     stdout=fnull, stderr=fnull) == 0
 
 
+    def _sshd_test(self, hostname, timeout=5):
+        """Verify whether sshd is running in host.
+
+        Args:
+          hostname: Hostname to verify.
+          timeout: Time in seconds to wait for a response.
+        """
+        try:
+            sock = socket.create_connection((hostname, 22), timeout=timeout)
+            sock.close()
+            return True
+        except socket.error:
+            return False
+
+
     def launch_client(self, info):
         """Launch a remote process on client and set up an xmlrpc connection.
 
@@ -230,7 +246,7 @@
         # Ensure old ssh connections are terminated.
         self._terminate_all_ssh()
         # Wait for the client to come up.
-        while timeout > 0 and not self._ping_test(self._client.ip):
+        while timeout > 0 and not self._sshd_test(self._client.ip):
             time.sleep(5)
             timeout -= 1
         assert timeout, 'Timed out waiting for client to reboot.'
@@ -238,10 +254,6 @@
         # Relaunch remote clients.
         for name, info in self._remote_infos.iteritems():
             if info['used']:
-                # This 5s delay to ensure sshd launched after network is up.
-                # TODO(waihong) Investigate pinging port via netcat or nmap
-                # to interrogate client for when sshd has launched.
-                time.sleep(5)
                 if install_deps:
                     if not self._autotest_client:
                         self._autotest_client = autotest.Autotest(self._client)