views:

41

answers:

1

Hey everyone,

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?

+1  A: 

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.

Marius Gedminas
The first command produced the same errors - I'll try using your pexpect and let you know. Thanks.
gnucom