cmd= ["sudo", "cat", "{filepath}".format(filepath=filepath), "|","egrep", "-v","\'{filteruser}\'".format(filteruser=filteruser)]
fileformat and filteruser can be blank too
[plugin_haproxy]
user= tara
host=localhost
filepath=/etc/haproxy/haproxy.global.inc
filter=
This is the command I want to run on subprocess, checking the above variable values with pdb shows bellow value and looks great
['sudo', 'cat', '/etc/haproxy/haproxy.global.inc', '|', 'egrep', '-v', "''"]
Manullay running the code sudo cat /etc/haproxy/haproxy.global.inc | egrep -v " '' " on terminal works great
Why is subprocess not able to process it.
"cat: |: No such file or directory\ncat: egrep: No such file or directory\ncat:
your shell will take a command such $ a | b | c
and turn it into three separate processes (a,b,c) and attach the output of one to the input of the next in the chain.
['sudo', 'cat', '/etc/haproxy/haproxy.global.inc', '|', 'egrep', '-v', "''"]
The above will execute the sudo
command (which will in-turn fork the cat
command and pass all the remaining args to cat
). It doesn't understand the "|" symbol. It just treats it as another arg. It is cat
that is complaining that it can not open "|", "egrep", etc at it is treating these as file names to cat.
You can try the Popen with shell=True
. I have not tried that and not sure if it will handle pipes.
The other option is to use Popen to execute the sudo
command only (no filter) and then use Popen.communicate
to read the output from the command in python and do the empty line filtering in python.
EDIT:
I did a quick check for the shell=True
. Here is the script below:
#!/usr/bin/env python
import subprocess
subprocess.call(["ls", "-l", "|", "cat"], shell=False)
shell=False
, I get the following error: ls: cat: No such file or directory
and ls: |: No such file or directory
. This is expected as ls
is trying to list the contents of |
and cat
.shell=True
, I get the desired output. That is, the output of ls
is piped to cat
. The shell is processing the |
.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With