CSIT-176 Fix interactive SSH console deadlock 22/3222/5
authorpmikus <pmikus@cisco.com>
Fri, 30 Sep 2016 13:29:08 +0000 (14:29 +0100)
committerMiroslav Miklus <mmiklus@cisco.com>
Wed, 5 Oct 2016 08:55:27 +0000 (08:55 +0000)
Change-Id: Iba291d6781238c0e880534195ff65a7b3c4bede1
Signed-off-by: pmikus <pmikus@cisco.com>
resources/libraries/python/ssh.py

index 287ad31..8acb54b 100644 (file)
@@ -21,7 +21,6 @@ import paramiko
 from paramiko import RSAKey
 from paramiko.ssh_exception import SSHException
 from scp import SCPClient
-from interruptingcow import timeout as icTimeout
 from robot.api import logger
 from robot.utils.asserts import assert_equal
 
@@ -213,19 +212,23 @@ class SSH(object):
         chan.get_pty()
         chan.invoke_shell()
         chan.settimeout(int(time_out))
+        chan.set_combine_stderr(True)
 
         buf = ''
-        try:
-            with icTimeout(time_out, exception=RuntimeError):
-                while not buf.endswith(':~$ '):
-                    if chan.recv_ready():
-                        buf = chan.recv(4096)
-        except RuntimeError:
-            raise Exception('Open interactive terminal timeout.')
+        while not buf.endswith(':~$ '):
+            try:
+                chunk = chan.recv(self.__MAX_RECV_BUF)
+                if not chunk:
+                    break
+                buf += chunk
+                if chan.exit_status_ready():
+                    logger.error('Channel exit status ready')
+                    break
+            except socket.timeout:
+                raise Exception('Socket timeout: {0}'.format(buf))
         return chan
 
-    @staticmethod
-    def interactive_terminal_exec_command(chan, cmd, prompt,
+    def interactive_terminal_exec_command(self, chan, cmd, prompt,
                                           time_out=30):
         """Execute command on interactive terminal.
 
@@ -247,13 +250,17 @@ class SSH(object):
         """
         chan.sendall('{c}\n'.format(c=cmd))
         buf = ''
-        try:
-            with icTimeout(time_out, exception=RuntimeError):
-                while not buf.endswith(prompt):
-                    if chan.recv_ready():
-                        buf += chan.recv(4096)
-        except RuntimeError:
-            raise Exception("Exec '{c}' timeout.".format(c=cmd))
+        while not buf.endswith(prompt):
+            try:
+                chunk = chan.recv(self.__MAX_RECV_BUF)
+                if not chunk:
+                    break
+                buf += chunk
+                if chan.exit_status_ready():
+                    logger.error('Channel exit status ready')
+                    break
+            except socket.timeout:
+                raise Exception('Socket timeout: {0}'.format(buf))
         tmp = buf.replace(cmd.replace('\n', ''), '')
         return tmp.replace(prompt, '')