【发布时间】:2014-01-08 01:17:02
【问题描述】:
我在 \tmp\ 位置有一个 python 文件,此文件打印一些内容并返回退出代码 22。我可以使用 putty 完美运行此脚本,但无法使用 paramiko 模块执行此操作。
这是我的执行代码
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()
这是我要在远程机器上执行的 python 脚本
#!/usr/bin/python
import sys
print "hello world"
sys.exit(20)
我无法理解我的逻辑实际上有什么问题。此外,当我执行 cd \tmp 然后执行 ls 时,我仍将位于根文件夹中。
【问题讨论】:
标签: linux bash python-2.7 ssh paramiko