X-Git-Url: https://gerrit.fd.io/r/gitweb?p=csit.git;a=blobdiff_plain;f=resources%2Flibraries%2Fpython%2Fssh.py;h=4b3838bbb79159212e8c0efa4193fb7f532a879c;hp=287ad31d657525b204cf6aab2dc83bd44cc055fd;hb=868380b0204758ef34c6be5bae0c9bdb00c4107f;hpb=a912d105f3a1d8fed0b4cf6b18e0ef7789be81bf diff --git a/resources/libraries/python/ssh.py b/resources/libraries/python/ssh.py index 287ad31d65..4b3838bbb7 100644 --- a/resources/libraries/python/ssh.py +++ b/resources/libraries/python/ssh.py @@ -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 @@ -196,7 +195,7 @@ class SSH(object): command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input) return self.exec_command(command, timeout) - def interactive_terminal_open(self, time_out=10): + def interactive_terminal_open(self, time_out=30): """Open interactive terminal on a new channel on the connected Node. :param time_out: Timeout in seconds. @@ -213,20 +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, - time_out=30): + def interactive_terminal_exec_command(self, chan, cmd, prompt): """Execute command on interactive terminal. interactive_terminal_open() method has to be called first! @@ -235,7 +237,6 @@ class SSH(object): :param cmd: Command to be executed. :param prompt: Command prompt, sequence of characters used to indicate readiness to accept commands. - :param time_out: Timeout in seconds. :return: Command output. .. warning:: Interruptingcow is used here, and it uses @@ -247,13 +248,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, '')