diff options
author | Marco Capitani <m.capitani@nextworks.it> | 2017-11-09 17:28:26 +0100 |
---|---|---|
committer | Marco Capitani <m.capitani@nextworks.it> | 2017-11-09 17:28:26 +0100 |
commit | 446cb363d9dd47fc759cff25c6c19fbe2bdc2bf0 (patch) | |
tree | 2b74572052166f2cc32c292dfce2524da2262bbb | |
parent | 667a7a587163c0ff5507c3a0188dea050681b84a (diff) | |
download | rumba-446cb363d9dd47fc759cff25c6c19fbe2bdc2bf0.tar.gz rumba-446cb363d9dd47fc759cff25c6c19fbe2bdc2bf0.zip |
SSH: added client.close() and better error handling
-rw-r--r-- | rumba/ssh_support.py | 52 |
1 files changed, 28 insertions, 24 deletions
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): |