Code Review
/
csit.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
review
|
tree
raw
|
inline
| side by side
HC Test: update URL paths for vpp-acl
[csit.git]
/
resources
/
libraries
/
python
/
ssh.py
diff --git
a/resources/libraries/python/ssh.py
b/resources/libraries/python/ssh.py
index
8acb54b
..
7b15998
100644
(file)
--- a/
resources/libraries/python/ssh.py
+++ b/
resources/libraries/python/ssh.py
@@
-29,6
+29,11
@@
__all__ = ["exec_cmd", "exec_cmd_no_error"]
# TODO: load priv key
# TODO: load priv key
+class SSHTimeout(Exception):
+ """This exception is raised when a timeout occurs."""
+ pass
+
+
class SSH(object):
"""Contains methods for managing and using SSH connections."""
class SSH(object):
"""Contains methods for managing and using SSH connections."""
@@
-118,7
+123,7
@@
class SSH(object):
:type timeout: int
:return return_code, stdout, stderr
:rtype: tuple(int, str, str)
:type timeout: int
:return return_code, stdout, stderr
:rtype: tuple(int, str, str)
- :raise
socket.t
imeout: If command is not finished in timeout time.
+ :raise
SSHT
imeout: If command is not finished in timeout time.
"""
start = time()
stdout = StringIO.StringIO()
"""
start = time()
stdout = StringIO.StringIO()
@@
-144,7
+149,7
@@
class SSH(object):
stderr.write(chan.recv_stderr(self.__MAX_RECV_BUF))
if time() - start > timeout:
stderr.write(chan.recv_stderr(self.__MAX_RECV_BUF))
if time() - start > timeout:
- raise
socket.t
imeout(
+ raise
SSHT
imeout(
'Timeout exception.\n'
'Current contents of stdout buffer: {0}\n'
'Current contents of stderr buffer: {1}\n'
'Timeout exception.\n'
'Current contents of stdout buffer: {0}\n'
'Current contents of stderr buffer: {1}\n'
@@
-195,7
+200,7
@@
class SSH(object):
command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input)
return self.exec_command(command, timeout)
command = 'sudo -S {c} <<< "{i}"'.format(c=cmd, i=cmd_input)
return self.exec_command(command, timeout)
- def interactive_terminal_open(self, time_out=
1
0):
+ def interactive_terminal_open(self, time_out=
3
0):
"""Open interactive terminal on a new channel on the connected Node.
:param time_out: Timeout in seconds.
"""Open interactive terminal on a new channel on the connected Node.
:param time_out: Timeout in seconds.
@@
-215,7
+220,7
@@
class SSH(object):
chan.set_combine_stderr(True)
buf = ''
chan.set_combine_stderr(True)
buf = ''
- while not buf.endswith(
':~$ '
):
+ while not buf.endswith(
(":~$ ", "~]$ ")
):
try:
chunk = chan.recv(self.__MAX_RECV_BUF)
if not chunk:
try:
chunk = chan.recv(self.__MAX_RECV_BUF)
if not chunk:
@@
-228,8
+233,7
@@
class SSH(object):
raise Exception('Socket timeout: {0}'.format(buf))
return chan
raise Exception('Socket timeout: {0}'.format(buf))
return chan
- def interactive_terminal_exec_command(self, 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!
"""Execute command on interactive terminal.
interactive_terminal_open() method has to be called first!
@@
-238,7
+242,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 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
:return: Command output.
.. warning:: Interruptingcow is used here, and it uses
@@
-262,7
+265,9
@@
class SSH(object):
except socket.timeout:
raise Exception('Socket timeout: {0}'.format(buf))
tmp = buf.replace(cmd.replace('\n', ''), '')
except socket.timeout:
raise Exception('Socket timeout: {0}'.format(buf))
tmp = buf.replace(cmd.replace('\n', ''), '')
- return tmp.replace(prompt, '')
+ for p in prompt:
+ tmp.replace(p, '')
+ return tmp
@staticmethod
def interactive_terminal_close(chan):
@staticmethod
def interactive_terminal_close(chan):
@@
-303,7
+308,7
@@
def exec_cmd(node, cmd, timeout=600, sudo=False):
ssh = SSH()
try:
ssh.connect(node)
ssh = SSH()
try:
ssh.connect(node)
- except Exception as err:
+ except
SSH
Exception as err:
logger.error("Failed to connect to node" + str(err))
return None, None, None
logger.error("Failed to connect to node" + str(err))
return None, None, None
@@
-313,7
+318,7
@@
def exec_cmd(node, cmd, timeout=600, sudo=False):
else:
(ret_code, stdout, stderr) = ssh.exec_command_sudo(cmd,
timeout=timeout)
else:
(ret_code, stdout, stderr) = ssh.exec_command_sudo(cmd,
timeout=timeout)
- except Exception as err:
+ except
SSH
Exception as err:
logger.error(err)
return None, None, None
logger.error(err)
return None, None, None