我在位置\ tmp \此文件中有一个python文件打印某些内容并返回退出代码22.我能够完美地运行此脚本腻子,但不能与paramiko模块做到这一点。如何通过ssh连接执行python或bash脚本并获取返回码
这是这是远程机器
#!/usr/bin/python
import sys
print "hello world"
sys.exit(20)
上执行我的Python脚本,我无法理解什么是我的逻辑其实是错误的我执行代码
import paramiko
def main():
remote_ip = '172.xxx.xxx.xxx'
remote_username = 'root'
remote_password = 'xxxxxxx'
remote_path = '/tmp/ab.py'
sub_type = 'py'
commands = ['echo $?']
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(remote_ip, username=remote_username,password=remote_password)
i,o,e = ssh_client.exec_command('/usr/bin/python /tmp/ab.py')
print o.read(), e.read()
i,o,e = ssh_client.exec_command('echo $?')
print o.read(), e.read()
main()
。另外当我做cd \ tmp然后ls时,我仍然会在根文件夹中。
感谢您的想法。我已经通过使用管道并将python脚本的输出重定向到一个文件然后在下一个命令中读取脚本来完成此任务。 – Hemant