From 446cb363d9dd47fc759cff25c6c19fbe2bdc2bf0 Mon Sep 17 00:00:00 2001 From: Marco Capitani Date: Thu, 9 Nov 2017 17:28:26 +0100 Subject: SSH: added client.close() and better error handling --- rumba/ssh_support.py | 52 ++++++++++++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 24 deletions(-) (limited to 'rumba') diff --git a/rumba/ssh_support.py b/rumba/ssh_support.py index 53965f8..e1d23bf 100644 --- a/rumba/ssh_support.py +++ b/rumba/ssh_support.py @@ -164,30 +164,34 @@ def execute_commands(testbed, ssh_config, commands, time_out=3): ssh_config.proxy_client = proxy_client o = "" - for command in commands: - logger.debug("%s@%s:%s >> %s" % (testbed.username, - ssh_config.hostname, - ssh_config.port, - command)) - envars = '. /etc/profile;' - command = envars + ' ' + command - chan = ssh_config.client.get_transport().open_session() - stdout = chan.makefile() - stderr = chan.makefile_stderr() - try: - chan.exec_command(command) - except paramiko.ssh_exception.SSHException as e: - raise SSHException('Failed to execute command') - o = _print_stream(stdout) - if chan.recv_exit_status() != 0: - e = _print_stream(stderr) - o_e = o + (('\n' + e) if e != "" else "") - raise SSHException('A remote command returned an error.' + - (('\n' + o_e) if o_e != "" else "")) - ssh_config.client = None - ssh_config.proxy_client = None - - return o + try: + for command in commands: + logger.debug("%s@%s:%s >> %s" % (testbed.username, + ssh_config.hostname, + ssh_config.port, + command)) + envars = '. /etc/profile;' + command = envars + ' ' + command + chan = ssh_config.client.get_transport().open_session() + stdout = chan.makefile() + stderr = chan.makefile_stderr() + try: + chan.exec_command(command) + except paramiko.ssh_exception.SSHException as e: + raise SSHException('Failed to execute command') + o = _print_stream(stdout) + if chan.recv_exit_status() != 0: + e = _print_stream(stderr) + o_e = o + (('\n' + e) if e != "" else "") + raise SSHException('A remote command returned an error.' + + (('\n' + o_e) if o_e != "" else "")) + return o + finally: + ssh_config.client.close() + ssh_config.proxy_client.close() + ssh_config.client = None + ssh_config.proxy_client = None + def execute_command(testbed, ssh_config, command, time_out=3): -- cgit v1.2.3