python子进程的自定义标准输入 [英] Custom standard input for python subprocess

查看:41
本文介绍了python子进程的自定义标准输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm running an SSH process like this:

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

这会打开一个交互式终端.根据 subprocess 的文档,sshproc 正在使用脚本的 sys.stdin.

This works and opens an interactive terminal. Based on the documentation for subprocess, sshproc is using the script's sys.stdin.

问题是:我如何将这个子进程接收到的输入打印到标准错误或文件?我正在创建一个日志 API,但目前无法记录通过此 SSH 会话运行的命令.

The question is: how can I print to stderr or a file what input is being received to this child process? I am creating a logging API, and currently lose the ability to record what commands are run over this SSH session.

我不需要答案,只需朝正确的方向推动即可.

I don't need the answer, just a nudge in the right direction.

谢谢大家!

重要的是我按照上面显示的方式开始这个过程,这样我就可以与我的用户进行交互式 SSH 会话.例如.据我所知,我无法使用 communicate().

It is important that I start the process as shown above so that I can have a interactive SSH session with my user. E.g. I cannot use communicate() as far as I know.

推荐答案

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)

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

Ah since you said nudge in right direction, I thought I should point you to good readups:

-

这篇关于python子进程的自定义标准输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆