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