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

sho*_*app 2 python stdin subprocess

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

sshproc = subprocess.Popen([command], shell=True)
exit = os.waitpid(sshproc.pid, 0)[1]
Run Code Online (Sandbox Code Playgroud)

这可以工作并打开一个交互式终端.根据文档subprocess,sshproc使用脚本sys.stdin.

问题是:如何向stderr或文件打印正在接收此子进程的输入?我正在创建一个日志API,并且目前无法记录在此SSH会话上运行的命令.

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

感谢大家!

编辑:重要的是我启动如上所示的过程,以便我可以与我的用户进行交互式SSH会话.例如communicate(),据我所知,我无法使用.

pyf*_*unc 7

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)
Run Code Online (Sandbox Code Playgroud)

啊,既然你说的朝着正确的方向推进,我想我应该指出你的好读数:

-