我读过很多例子,但没有一个能完成这个特定的任务。
Python代码:
x = Popen(commands, stdout=PIPE, stderr=PIPE, shell=True)
print commands
stdout = x.stdout.read()
stderr = x.stderr.read()
print stdout, stderr
return stdout
输出:
[user@host]$ python helpers.py
['ssh', '-t', 'user@host', ' ', "'service --status-all'"]
usage: ssh [-1246AaCfgKkMNnqsTtVvXxYy] [-b bind_address] [-c cipher_spec]
[-D [bind_address:]port] [-e escape_char] [-F configfile]
[-I pkcs11] [-i identity_file]
[-L [bind_address:]port:host:hostport]
[-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
[-R [bind_address:]port:host:hostport] [-S ctl_path]
[-W host:port] [-w local_tun[:remote_tun]]
[user@]hostname [command]
为什么我会犯这个错误? 使用os.popen(…)它可以工作,至少可以执行,但我无法通过SSH隧道检索远程命令的输出。
我认为你的命令列表是错误的:
另外,如果要将列表传递给
Popen
,我认为不应该传递shell=True
。例如,执行以下任一操作:
或者这个:
但不是这个:
最后,还有一个方便的函数,可以像shell一样将字符串拆分为列表:
将返回:
相关问题 更多 >
编程相关推荐