Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

subprocess not running the command generated though the command works on terminal

cmd= ["sudo", "cat", "{filepath}".format(filepath=filepath), "|","egrep", "-v","\'{filteruser}\'".format(filteruser=filteruser)]

fileformat and filteruser can be blank too

config file below

[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:

like image 976
Tara Prasad Gurung Avatar asked Mar 29 '17 05:03

Tara Prasad Gurung


1 Answers

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)
  • With 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.
  • with shell=True, I get the desired output. That is, the output of ls is piped to cat. The shell is processing the |.
like image 98
Ali Tajeldin Avatar answered Oct 28 '22 13:10

Ali Tajeldin