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']
Share:
10,419
StefanS
Author by

StefanS

Updated on June 04, 2022

Comments

  • StefanS
    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
      synthesizerpatel over 12 years
      Don'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
      StefanS over 12 years
      hostname = 'host' Do you mean that ^? That is my test for filling the variable with a test hostname.
  • StefanS
    StefanS over 12 years
    commandargs = [ '/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
    AKX over 12 years
    Using a pipeline won't work without the shell argument, but if you're only using sed to remove a line, you can just as well do that in Python too.
  • StefanS
    StefanS over 12 years
    Sorry for my pastings, :-(, I tried to put edited list in here.
  • StefanS
    StefanS over 12 years
    Okay, much thanks. But how do I can I use a blank in an list element?
  • AKX
    AKX over 12 years
    something = ["", "", ""] will create a list of three empty strings.
  • StefanS
    StefanS over 12 years
    thanks, that is working fine. Only the "last day" has an blank in it. Is there a special character for it?
  • StefanS
    StefanS over 12 years
    Please 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
    phihag over 12 years
    @StefanS No, the point of the list is precisely to let subprocess do all the escaping.
  • StefanS
    StefanS over 12 years
    Ok, and what is with a character like ', do I have to escape it?
  • AKX
    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
    StefanS over 12 years
    I have to say, that stackoverflow really helped me and that at my first post! hurray on stackoverflow and rocking python!
  • phihag
    phihag over 12 years
    No. 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.