开发者

Virtuozzo and automating commands with Python's subprocesses

I'm dealing with a Virtuozzo server and want to automate logging into each container and issuing a few commands in Python by creating a subprocess for 'vzctl enter '.

Here is the snippet that I'm working on right now -

#!/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'

But the output I see everytime is -

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

I really think this is a BASH error, can anyone give me a suggestion?


Looks like vzctl expects stdin/stdout to be a terminal. You can find out which by experimenting (in bash):

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

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

You can use the pty module from the standard library to create pseudottys, but that module is very low-level.

There's a 3rd-party module called pexpect that might fit the bill.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜