0

Bit about the setup and goal first; I have Core installed and a session running that I wish to run a few commands on from a python script. Since there are only a few commands and they are fairly simple commands, figured I'd just use subprocess.Popen to kick of coresendmsg instead of trying to implement the core control plane in my script.

An example of the command (as run from the CLI) is as follows:

coresendmsg exec node=2 num=1001 cmd='ifconfig eth0 192.168.0.1'

I've had some issues with running this command from python and having coresendmsg properly parse the cmd argument. If run as seen below, coresendmsg considers eth0 and 192.168.0.1 as separate arguments and chokes, which makes sense to me.

s=subprocess.Popen(['coresendmsg', 
    'exec',
    '-s 47854',
    'node=2',
    'num=1001',
    'cmd=ifconfig eth0 192.168.0.1'])

If I then change it to the following, coresendmsg now considers the cmd as a single argument, but still chokes because the single quotes are now included when the arguments are split up in coresendmsg. This is different then when the coresendmsg is run from the commandline with the same quotes, as seen in the example above.

s=subprocess.Popen(['coresendmsg', 
    'exec',
    '-s 47854',
    'node=2',
    'num=1001',
    'cmd=\'ifconfig eth0 192.168.0.1\''])

I know this because I went in and modified coresendmsg to print the arguments and checked both cases. When the command above run from CLI, I see this:

['exec', 'node=2', 'num=1001', 'cmd=ifconfig eth0 192.168.0.1']

but when run from python I get one of the following depending on how i quote things:

['exec', 'node=2', 'num=1001', 'cmd=ifconfig', 'eth0', '192.168.0.1']
['exec', 'node=2', 'num=1001', "cmd='ifconfig eth0 192.168.0.1'"]
['exec', 'node=2', 'num=1001', 'cmd="ifconfig eth0 192.168.0.1"']

I can make this work from a bash script but I'd rather not run a script, from a script, from a script. Looking for any suggestions that might resolve this problem.

1
  • 2
    I noticed you have '-s 47854'. Try to change that to '-s', '47854' Commented May 28, 2016 at 6:29

1 Answer 1

2

To clarify what @HaiVu says, your first example fails because of -s 47854, not the cmd parameter. In your example a single parameter of "-s 47854" was being passed. Depending on how arguments in the program are parsed, it could well take node=2 as the value for the -s.

Since you are using a list, spaces do not separate arguments, so:

s=subprocess.Popen(['coresendmsg',
    'exec',
    '-s', '47854',
    'node=2',
    'num=1001',
    'cmd=ifconfig eth0 192.168.0.1'])

Your experiments with quoting differences are to be expected. When you run from a shell ("command line") the shell strips away quotes. Running from Python also strips quotes if you think about it. If you escape quotes they are passed directly, both in python and bash.

If you need additional shell behaviour then use shell=True parameter to Popen. In this case that is not required, and can be avoided in most cases.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.