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
- replace all
\
by \\
- 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
or
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\\\"'\""
ProxyCommand
for SSH forwarding? – Ignacio Vazquez-Abrams Nov 07 '12 at 06:28