我正在处理 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/