Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Possible Duplicate:
Quoting in ssh $host $FOO and ssh $host “sudo su user -c $FOO” type constructs
Wrapping a command that includes single and double quotes for another command

I want to run sql over ssh from php . Below command runs nicely

ssh server1 "sudo -u db2inst1 sh -c '/opt/ibm/db2/V9.7/bin/db2 connect to RATIONAL; /opt/ibm/db2/V9.7/bin/db2 set schema Edumate; /opt/ibm/db2/V9.7/bin/db2 \"select * from edumate_settings\"'"

Now I need to do ssh from server1 to server2 and then run "sudo -u db2inst1 .... " there.

Is there any easy way how to deal with quotes if I add additional server to this scenario? So it will start like

ssh server1 "ssh server2 "the rest is as above"

Note the I need to run above code from php.

share|improve this question

marked as duplicate by Gilles, Renan, rahmu, jasonwryan, Mat Nov 8 '12 at 6:49

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

How come you're not using ProxyCommand for SSH forwarding? – Ignacio Vazquez-Abrams Nov 7 '12 at 6:28
Because I don't know about it :-) Let me check it tomorrow.. – Radek Nov 7 '12 at 6:47
up vote 2 down vote accepted

You want to get from command_with_arguments to command "escaped_command_with_arguments". You get escaped_command_with_arguments by scanning command_with arguments from left to right and replacing each \ by \\ and each " by \".

alternatively you put command_with_arguments in an editor and

  1. replace all \ by \\
  2. replace each " by \"

in this order. So step by step you can generate the following sequence of expressions (I used windows notepad to create the expression)

select *
db2 "select *"
sudo "db2 \"select *\""
ssh "sudo \"db2 \\\"select *\\\"\""
ssh "ssh \"sudo \\\"db2 \\\\\\\"select *\\\\\\\"\\\"\""
ssh "ssh \"ssh \\\"sudo \\\\\\\"db2 \\\\\\\\\\\\\\\"select *\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""
ssh "ssh \"ssh \\\"ssh \\\\\\\"sudo \\\\\\\\\\\\\\\"db2 \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"select *\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\"\\\\\\\\\\\\\\\"\\\\\\\"\\\"\""

If you count the numbers of \ before each " in the last expression you get the following seuqence:

0 1 3 7 15 31 31 15 7 3 1 0


1-1 2-1 4-1 8-1 16-1 32-1 32-1 16-1 8-1 4-1 2-1 1-1

So it is possible to construct such a nested expression in one step without iteration, but you must take care how the expressions are nested.

Also these methods can be extended to situations where there are other escaped characters besides " and \.

So put your command in an editor, replace server1 by server2, replace \ by \\ and " by \" as described above and add ssh server1 " at the beginning and " at the end and you will get

ssh server1 "ssh server2 \"sudo -u db2inst1 sh -c '/opt/ibm/db2/V9.7/bin/db2 connect to RATIONAL; /opt/ibm/db2/V9.7/bin/db2 set schema Edumate; /opt/ibm/db2/V9.7/bin/db2 \\\"select * from edumate_settings\\\"'\""
share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.