【问题标题】:Custom standard input for python subprocesspython子进程的自定义标准输入
【发布时间】:2011-04-13 08:42:54
【问题描述】:

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

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

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

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

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

谢谢大家!

编辑: 重要的是,我开始如上所示的过程,以便我可以与我的用户进行交互式 SSH 会话。例如。据我所知,我不能使用communicate()

【问题讨论】:

    标签: python subprocess stdin


    【解决方案1】:
    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)
    

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

    -

    【讨论】:

    • 感谢您的建议,但在使用communicate 时,我的用户无法交互使用 SSH 终端。
    猜你喜欢
    • 1970-01-01
    • 2012-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多