def ssh_exec_command(ssh_obj, cmd, prefix=''):
"""
Execute a command on the ssh connection.
:param ssh_obj: SSH object.
:param cmd: command to run.
:param prefix: Prefix to be used for printing
:return: stdout and stderr
"""
try:
print(prefix+ "[*] Running Command:" + cmd)
_, stdout_obj, stderr_obj = ssh_obj.exec_command(cmd)
exit_status = stdout_obj.channel.recv_exit_status()
if exit_status == 0:
print(prefix + GREEN_PRINT + "[+] Command:" + cmd + " Completed Successfully" + END_PRINT)
else:
print(prefix + RED_PRINT + "[*] Command:" + cmd + " Return Code:" + str(exit_status) + END_PRINT)
except paramiko.SSHException as e:
print(prefix + RED_PRINT + "[!] Problem occurred while running command: %s, Error: %s" % (cmd, e) + END_PRINT)
raise e
return stdout_obj, stderr_obj
评论列表
文章目录