The subprocess does not run the command generated, although the command works on the terminal

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

fileformat and filteruser may also be empty

configuration file below

[plugin_haproxy]
user= tara
host=localhost
filepath=/etc/haproxy/haproxy.global.inc
filter=

This is the command I want to run under the subprocess , checking the above variable values ​​with pdb , displaying the next value and looking great

['sudo', 'cat', '/etc/haproxy/haproxy.global.inc', '|', 'egrep', '-v', "''"]

Manullay runs sudo cat / etc / haproxy / haproxy.global.inc code | egrep -v "''" on the terminal works fine

Why the subprocess is not able to process it.

"cat: |: There is no such file or directory \ ncat: egrep: There is no such file or Directory \ NCAT:

+4
source share
3 answers

$ a | b | c (a, b, c) .

['sudo', 'cat', '/etc/haproxy/haproxy.global.inc', '|', 'egrep', '-v', "''"]

sudo ( cat cat). "|" . . cat, , "|", "egrep" .., cat.

Popen shell=True. , .

- Popen sudo ( ), Popen.communicate python python.

EDIT: shell=True. script:

#!/usr/bin/env python
import subprocess

subprocess.call(["ls", "-l", "|", "cat"], shell=False)
  • shell=False : ls: cat: No such file or directory ls: |: No such file or directory. , ls | cat.
  • shell=True, . ls cat. |.
+2

shell=True, , subprocess pipe:

p1 = subprocess.Popen(['sudo', 'cat', '/etc/haproxy/haproxy.global.inc'], stdout=subprocess.PIPE)
p2 = subprocess.Popen(['egrep', '-v', "''"], stdin=p1.stdout,stdout=subprocess.PIPE)
0

command = ["sudo", "cat", "{filepath}".format(filepath=filepath)]         
userfilter = ["egrep", "-v", "\"{filteruser}\"".format(filteruser=filteruser)***]

Subprocess

cmdout = subprocess.Popen(command, stdout=subprocess.PIPE)
filtration = subprocess.Popen(runfilter, stdin=cmdout.stdout,stdout=subprocess.PIPE)

output, err = filtration.communicate()

, , , Python

0
source

Source: https://habr.com/ru/post/1673459/


All Articles