Python - using a list in Popen as command
10,419
Solution 1
Each string in the given list is handled as a single command line argument. You don't need to use quotes when using this syntax either.
Try something like this:
hostname = 'host'
servername = 'server'
commandargs = [
'/usr/sbin/mminfo',
'-o', 'n', # these are separate arguments, but on the same line for clarity's sake
'-s', servername, # same here
'-q', "client='%s',savetime>=last day" % hostname, # same here...
'-r', 'client,name' # and here.
]
process = subprocess.Popen(commandargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
EDIT: Or, based on comments, something like
import subprocess
client_name = "lxds05"
server_name = "nsr_srv"
queryspec = "client='%s',savetime>=last day" % client_name
reportspec = "client,name,savetime(17),nsavetime,level,ssflags"
args = [
'/usr/sbin/mminfo',
'-o', 'n',
'-s', server_name,
'-q', queryspec,
'-r', reportspec,
'-x', 'c'
]
subprocess.Popen(args) # ... etc
Solution 2
In the list, every argument must be its own item. There should be no spaces in the strings:
commandargs = ['/usr/sbin/mminfo', '-o', 'n', '-s', servername,
'-q', "client='" + hostname + "',savetime>=last day",
'-r', 'client,name']
Author by
StefanS
Updated on June 04, 2022Comments
-
StefanS almost 2 years
I try to create a subprocess with Popen. Here is my code at first:
hostname = 'host' servername = 'server' commandargs = ['/usr/sbin/mminfo',' -o n',' -s',servername,' -q "client=\'',hostname,'\',savetime>=last day"',' -r "client,name"'] process = subprocess.Popen(commandargs, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
The problem is that the executed command failed with a message, that the contacted server is not available. It seems that the variable hostname is not used... Trying the same with a string, instead of a list, as command in Popen (with Shell=True) everything is working fine.
Does anyone know what is wrong with the code?
Regards. Stefan
-
synthesizerpatel over 12 yearsDon't quote the hostname, no need. Valid hostnames don't have any characters that could expand to something weird, nor do they have spaces. The other thing you should do is have each entity in the command seperate, don't do ' -o n', but instead '-o','n', etc.
-
StefanS over 12 yearshostname = 'host' Do you mean that ^? That is my test for filling the variable with a test hostname.
-
-
StefanS over 12 yearscommandargs = [ '/usr/sbin/mminfo', '-o','n', '-s',servername, '-q','"client='',hostname,'\',savetime>=last',' ','day"', '-r','"client,name,savetime(17),nsavetime,level,ssflags"', '-x','"c;"', '|sed','"1d"']
-
AKX over 12 yearsUsing a pipeline won't work without the
shell
argument, but if you're only usingsed
to remove a line, you can just as well do that in Python too. -
StefanS over 12 yearsSorry for my pastings, :-(, I tried to put edited list in here.
-
StefanS over 12 yearsOkay, much thanks. But how do I can I use a blank in an list element?
-
AKX over 12 years
something = ["", "", ""]
will create a list of three empty strings. -
StefanS over 12 yearsthanks, that is working fine. Only the "last day" has an blank in it. Is there a special character for it?
-
StefanS over 12 yearsPlease have a look at this output of my script. ./nwcheck.py -H lxds05 -c 1 ['/usr/sbin/mminfo', '-o', 'n', '-s', 'nsr_srv', '-q', '"client=\'', 'lxds05', "'savetime>=last", '', 'day"', '-r', '"client,name,savetime(17),nsavetime,level,ssflags"', '-x', '"c;"'] ('unknown report constraint: "client\nusage: mminfo [-avV] [-o order] [-s server] [-x exportspec] [report] [query] [volname...]\n\n<report>: [ -m | -p | -B | -S | -X | -r reportspec ]\n<query>: [-c client] [-N name] [-t time] [-q queryspec]\n', '') I tried to escape the "'" with an backslash like this \', seems not to work...
-
phihag over 12 years@StefanS No, the point of the list is precisely to let subprocess do all the escaping.
-
StefanS over 12 yearsOk, and what is with a character like ', do I have to escape it?
-
AKX over 12 years@StefanS - you're welcome! And welcome to StackOverflow (and maybe welcome to the wonderful world of Python too, if you're new here :) ). Be sure to accept either of the answers as correct too!
-
StefanS over 12 yearsI have to say, that stackoverflow really helped me and that at my first post! hurray on stackoverflow and rocking python!
-
phihag over 12 yearsNo. As I wrote, subprocess does all the escaping for a command-line invocation. If your program interprets its arguments as a representation of a compound structure (like
-q
in your example, which I didn't notice before - updated), you have to assemble that representation yourself.