1

In twisted, getProcessOutput method could get 'ps' shell command ouput by using getProcessOutupt('ps', 'aux') and return a defer.

my question is how to run command like "ps aux | grep 'some keyword' | awk '{...}'" in getProcessOutput. for example getProcessOutput("ps aux | grep 'some keyword' | awk '{...}'").

any suggestions would be appreciated.

Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28
  • A really ugly hack would be to write the entire command pipeline into a string suitable to be passed to `sh -c "..."` as a single argument. Then your call would be something like: getProcessOutput('sh', ('-c', "ps aux | grep 'some keyword' | awk '{...}'"), *other_getProd_args) – Jim Dennis Mar 10 '15 at 06:36
  • @JimDennis, thanks for your answer. :-) . I want to get command output from some remote computers by using perspective broker server which use getProcessOutput to run local shell command. do you have any ideas about the best way to do it ? – Fujiao Liu Mar 10 '15 at 06:58
  • 1
    If you actually want shell pipelines, `["/bin/sh", "-c", "..."]` is not an ugly hack, it's the right way to do it. – Glyph Mar 10 '15 at 18:33
  • If what you want is to get output from a remote computer and send it to a local program, what you want is to implement `def remote_something(self, some_bytes)` to call `write` on the `ProcessTransport` associated with the `ProcessProtocol` you spawned using `spawnProcess`. If you want to ask a question about perspective broker though, it's not about shell pipelines, so please ask a separate question :) – Glyph Mar 10 '15 at 18:34

1 Answers1

0

use getProcessOutput('/bin/sh', ('-c', cmd)). cmd is your shell command. try it :-)

Fujiao Liu
  • 2,195
  • 2
  • 24
  • 28