0

I'm writing qemu qga command through python script like this:

python cmd.py request:'{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk '{print $2}'"]}]}}'

but the param is parsed into bellow, which devide json string from '{print $2}' :

['cmd.py', 'request:{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk {print', '}"]}]}}']

actually I want this, which awk {print'{print $2}' is shell command running in vm, and this format could not change:

['cmd.py', 'request:{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk {print'{print $2}'}"]}]}}']

Is it possible? If it's impossible, it means I have to construct json string by hand in cmd.py?

Thank you~

1
  • You have to remove spaces and use backslash for your quotes. This way, your entire json string will be seen as just one argument. Commented Aug 11, 2017 at 9:25

1 Answer 1

1

You accidentally embedded single quotes in other single quotes: If I leave out the first part of the string you get the following:

request:'... | awk '{print $2}'"]}]}}'

Which the shell sees as three parts, and tries to resolve the $2 there:

request:'... | awk ' {print $2} '"]}]}}'

If you want to embed single quotes in other single quotes, you have to do it like this:

echo 'This string contains '\'' <- an embedded quote'

So in your case the full command should be

python cmd.py request:'{"execute": "guest-execute", "session": "4768", "arguments": {"commands": [{"command": "/bin/sh", "arguments": ["sh", "-c", "route | grep default | awk '\''{print $2}'\''"]}]}}'

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.