Srinivas Gadi
Srinivas Gadi

Reputation: 117

How to display single quotes inside double quotes in string in python

I know this is very basic question, but not able to frame it out.

I need to execute a customized python command based command with in a script.

ex:
below is the actual commands.
command-dist --host='server1' -- cmd -- 'command1;command2'

I need to use this command in my python script form my script , this command needs be to executed on remote server

I need this command format to use in my script

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

but the above is failing due to quotes, tried number of ways still no luck.

When I tried this way
cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

I don't under why back slashes are appearing before and after cmd1;cmd2?
This is what I am not getting.
cmd
'ssh -q %s "command-dist --host=%s -- cmd -- /'cmd1;cmd2'/""' %(host1,host2)

Please help how do I get the above value

Upvotes: 0

Views: 349

Answers (1)

JulienD
JulienD

Reputation: 7293

This

cmd="ssh -q %s "command-dist --host=%s -- cmd -- 'cmd1;cmd2'"" %(host1,host2)

is understood by Python as

cmd = <some string>command-dist --host=%s -- cmd -- 'cmd1;cmd2'<some string>

because you use the same quotes inside and outside. Try instead to escape the internal quotes with a backslash:

cmd="ssh -q %s \"command-dist --host=%s -- cmd -- 'cmd1;cmd2'\"" %(host1,host2)

Aside from that, you should use the subprocess module and supply your command as a list to subprocess.Popen.

Upvotes: 1

Related Questions