[Twisted-Python] How to emulate Python's getoutput function
Is there a relatively simple way to emulate the following python function: commands.getoutput ('ifconfig eth0 | grep inet')) I can get halfway there with: utils.getProcessOutput ('/sbin/ifconfig', ['eth0']) but the trick to piping that output to another command such as grep is eluding me. Thank you.
On Fri, 07 Oct 2005 23:04:01 -0400, Drake Smith <drakesmith@adelphia.net> wrote:
Is there a relatively simple way to emulate the following python function: commands.getoutput ('ifconfig eth0 | grep inet'))
For the particular case of grep, this is pretty straightfoward: def gotOutput(output): lines = output.splitlines() for L in lines: if 'inet' in L: yield L utils.getProcessOutput(...).addCallback(gotOutput).addCallback(something) For the general case of chaining processes, you need to get down to reactor.spawnProcess(). This API lets you specify which file descriptors are connected to what. A pipe ends up being just that - use os.pipe() to create one, hand one end to the stdout of one process and the other end to the stdin of another, and now they're talking :) Hope this helps, Jp
On 10/8/05, Drake Smith <drakesmith@adelphia.net> wrote:
Is there a relatively simple way to emulate the following python function: commands.getoutput ('ifconfig eth0 | grep inet'))
I can get halfway there with: utils.getProcessOutput ('/sbin/ifconfig', ['eth0']) but the trick to piping that output to another command such as grep is eluding me. Thank you.
make the command "sh", and the arguments ['-c', 'shell string']. Of course, this means you're opening yourself up to shell insertion attacks if you put any user-input into that shell string. -- Twisted | Christopher Armstrong: International Man of Twistery Radix | -- http://radix.twistedmatrix.com | Release Manager, Twisted Project \\\V/// | -- http://twistedmatrix.com |o O| | w----v----w-+
participants (3)
-
Christopher Armstrong
-
Drake Smith
-
Jp Calderone