python子进程的自定义标准输入

2024-06-17 11:05:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在运行这样的SSH进程:

sshproc = subprocess.Popen([command], shell=True)
exit = os.waitpid(sshproc.pid, 0)[1]

这样可以工作并打开一个交互式终端。基于subprocess的文档,sshproc使用脚本的sys.stdin

问题是:我如何才能打印到stderr或一个文件接收到此子进程的哪些输入?我正在创建一个日志API,目前无法记录在此SSH会话中运行的命令。

我不需要答案,只要朝正确的方向轻轻一推。

谢谢大家!

编辑:如上图所示启动进程,以便与用户进行交互式SSH会话,这一点很重要。E、 据我所知,我不能用communicate()


Tags: 文档脚本true终端进程osexitshell
1条回答
网友
1楼 · 发布于 2024-06-17 11:05:01
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)

啊,既然你说的是朝着正确的方向轻推,我想我应该给你指一些好的读物:

-

相关问题 更多 >