python - Virtuozzo 和使用 Python 子进程的自动化命令

标签 python subprocess virtuozzo

我正在处理 Virtuozzo 服务器,希望通过为“vzctl Enter”创建子进程来自动登录到每个容器并在 Python 中发出一些命令。

这是我现在正在处理的代码片段 -

#!/usr/bin/python

import subprocess

print 'Start'
proc = subprocess.Popen(['vzctl enter 123'], 
                             stdout=subprocess.PIPE, 
                             stdin=subprocess.PIPE,
                             shell=True)
print proc.communicate('whoami')[0]
print 'Finished'

但是我每次看到的输出都是 -

Unable to get term attr: Invalid argument
Unable to restore term attr: Invalid argument

我真的认为这是一个 BASH 错误,有人能给我建议吗?

最佳答案

看起来 vzctl 期望 stdin/stdout 成为终端。您可以通过实验(在 bash 中)找出答案:

$ echo whoami | vzctl enter 123  # stdin is not a tty

$ vzctl enter 123 | cat          # stdout is not a tty
whoami
<ctrl-d>

您可以使用标准库中的pty模块来创建pseudotty,但该模块的级别非常低。

有一个名为 pexpect 的第三方模块可能符合要求。

关于python - Virtuozzo 和使用 Python 子进程的自动化命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3198617/

相关文章:

python - 如何停止将 SIGINT 传递给 python 中的子进程?

python - Python 导演类中的 Swig 异常

python - GA童话世代

python - 如何通过拖动 slider 来控制我的 Kivy ScrollView ?

python kill子进程在退出时进行通信

elasticsearch - elasticsearch vserver安装错误

python - 使用 ctypes 使用 Ctrl-C 中断 Python C 扩展

python - 在 Python 中使用 g++ 和子进程

php - 如何在运行 Plesk Virtuozzo 的服务器上升级 php?

php - PHP 如何使用超过 100% 的 CPU?