Rdit
Rdit

Reputation: 1562

Send complex command via ssh

I'm trying to send this command via ssh:

ssh <user1>@<ip1>  ssh <user2>@<ip2> /opt/user/bin -f /opt/user/slap.conf -l /home/admin/`date +%Y%m%d`_Export_file$nr.gz -s "ou=multi" -a "(& (entry=$nr)(serv=PS))" -o wrap=no

this command is customized so do not confuse with this... But it's not executed, smth like: unexpected '(

If i log in to the server and i give this command it gets executed correctly. So i think it should be something with bracket and parentheses rules.

Please can someone help me?

thank you in advance.

Upvotes: 1

Views: 1122

Answers (1)

chiastic-security
chiastic-security

Reputation: 20520

You will need to escape the quotes, possibly twice, since each invocation of ssh will involve stripping a layer off. Put escaped single quotes round the entire command, and then nested unescaped single quotes round the inner command:

ssh <user1>@<ip1>  \'ssh <user2>@<ip2> '/opt/user/bin -f /opt/user/slap.conf -l /home/admin/`date +%Y%m%d`_Export_file$nr.gz -s "ou=multi" -a "(& (entry=$nr)(serv=PS))" -o wrap=no'\'

This assumes, by the way, that you want the backticks to be unpacked and the command executed on ip2, rather than beforehand on your source machine, and similarly with the decoding of the $nr variable. It's not clear how you want them interpreted.

Upvotes: 1

Related Questions