abstract_ssh.py: Write known hosts to temp filedescriptor

Implementing the approach suggested by Jongki Suwandi to
resolve the multiple messages like:

[stderr] Warning: Permanently added '192.168.122.99' (RSA) to the list of known hosts.

By redirecting them to a filedescriptor of a temporary
file, once an abstract ssh instance finishes, the file
is unlinked, so we get rid of all fingerprint registers.

This way we're able to drop the -q flag to the base ssh
command, and we have only one message like the above per
autoserv execution.

Signed-off-by: Lucas Meneghel Rodrigues <[email protected]>


git-svn-id: http://test.kernel.org/svn/autotest/trunk@4207 592f7852-d20e-0410-864c-8624ca9c26a4
diff --git a/server/hosts/abstract_ssh.py b/server/hosts/abstract_ssh.py
index e3bfe61..e386b53 100644
--- a/server/hosts/abstract_ssh.py
+++ b/server/hosts/abstract_ssh.py
@@ -10,15 +10,16 @@
                                                     type=bool, default=False)
 
 
-def make_ssh_command(user="root", port=22, opts='', connect_timeout=30,
-                     alive_interval=300):
-    base_command = ("/usr/bin/ssh -a -q -x %s -o StrictHostKeyChecking=no "
-                    "-o UserKnownHostsFile=/dev/null -o BatchMode=yes "
+def make_ssh_command(user="root", port=22, opts='', hosts_file='/dev/null',
+                     connect_timeout=30, alive_interval=300):
+    base_command = ("/usr/bin/ssh -a -x %s -o StrictHostKeyChecking=no "
+                    "-o UserKnownHostsFile=%s -o BatchMode=yes "
                     "-o ConnectTimeout=%d -o ServerAliveInterval=%d "
                     "-l %s -p %d")
     assert isinstance(connect_timeout, (int, long))
     assert connect_timeout > 0 # can't disable the timeout
-    return base_command % (opts, connect_timeout, alive_interval, user, port)
+    return base_command % (opts, hosts_file, connect_timeout,
+                           alive_interval, user, port)
 
 
 # import site specific Host class
@@ -44,6 +45,9 @@
         self.port = port
         self.password = password
         self._use_rsync = None
+        self.known_hosts_file = os.tmpfile()
+        known_hosts_fd = self.known_hosts_file.fileno()
+        self.known_hosts_fd = '/dev/fd/%s' % known_hosts_fd
 
         """
         Master SSH connection background job, socket temp directory and socket
@@ -94,8 +98,9 @@
         appropriate rsync command for copying them. Remote paths must be
         pre-encoded.
         """
-        ssh_cmd = make_ssh_command(self.user, self.port,
-                                   self.master_ssh_option)
+        ssh_cmd = make_ssh_command(user=self.user, port=self.port,
+                                   opts=self.master_ssh_option,
+                                   hosts_file=self.known_hosts_fd)
         if delete_dest:
             delete_flag = "--delete"
         else:
@@ -116,8 +121,8 @@
         pre-encoded.
         """
         command = ("scp -rq %s -o StrictHostKeyChecking=no "
-                   "-o UserKnownHostsFile=/dev/null -P %d %s '%s'")
-        return command % (self.master_ssh_option,
+                   "-o UserKnownHostsFile=%s -P %d %s '%s'")
+        return command % (self.master_ssh_option, self.known_hosts_fd,
                           self.port, " ".join(sources), dest)
 
 
@@ -512,6 +517,7 @@
     def close(self):
         super(AbstractSSHHost, self).close()
         self._cleanup_master_ssh()
+        self.known_hosts_file.close()
 
 
     def _cleanup_master_ssh(self):