我正在运行这样的 SSH 进程:
sshproc = subprocess.Popen([command], shell=True)
exit = os.waitpid(sshproc.pid, 0)[1]
这将起作用并打开一个交互式终端。基于以下文档subprocess
, sshproc
正在使用脚本的sys.stdin
.
问题是:如何将这个子进程收到的输入打印到 stderr 或文件?我正在创建一个日志记录 API,目前无法记录在此 SSH 会话上运行的命令。
我不需要答案,只需要朝着正确的方向轻推即可。
感谢大家!
EDIT:启动如上所示的过程非常重要,这样我就可以与我的用户进行交互式 SSH 会话。例如。我无法使用communicate()
据我所知。
sshproc = subprocess.Popen([command],
shell=True,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
stdout_value, stderr_value = sshproc.communicate('through stdin to stdout')
print repr(stdout_value)
print repr(stderr_value)
啊,既然你说朝正确的方向轻推,我想我应该给你指出一些好的读物:
- http://www.doughellmann.com/PyMOTW/subprocess/ http://www.doughellmann.com/PyMOTW/subprocess/
- 从 python subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE) 捕获 stderr https://stackoverflow.com/questions/914320/capture-stderr-from-python-subprocess-popencommand-stderrsubprocess-pipe-std
-
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)