0

I'm using the below command in a script:

ssh user@<ip> "ssh user1@<ip1> 'df -k /local/ | awk '{print \$4}' | tail -1'" 

so it escapes once the special character '$' but it sends the command as below in the second server:

debug1: Sending command: df -k /local/ | awk {print } | tail -1 awk: cmd. line:1: {print awk: cmd. line:1: ^ unexpected newline or end of string 

can someone help me with a solution?

3
  • Is there some authentication for the second server?? Commented Nov 12, 2014 at 15:26
  • no authentication. keys exchanged. Commented Nov 12, 2014 at 15:27
  • Look into using ssh's ProxyCommand option, so that you can connect "directly" to <ip1> from your local host. Commented Nov 12, 2014 at 17:10

3 Answers 3

1

There is no need for the exterior quotes in this particular case, actually.

ssh user@<ip> ssh user1@<ip1> "df -k /local/ | awk '{print \$4}' | tail -1" 

But there is also no reason to run the postprocessing on the remote host.

ssh user@<ip> ssh user1@<ip1> df -k /local/ | awk '{print $4}' | tail -1 

You can also optimize out the tail:

ssh user@<ip> ssh user1@<ip1> df -k /local/ | awk '{x=$4}END{print x}' 

In the general case, if you need to mix both double and single quotes, you can backslash-escape double quotes inside double quotes, but single quotes inside single quotes are not possible.

ssh user@<ip> "ssh user1@<ip1> \"df -k /local/ | awk '{x=\$4}END{print x}'\"" 
Sign up to request clarification or add additional context in comments.

Comments

1

Here document would be usefull

ssh user@<ip> <<EOIP ssh user1@<ip1> <<EOIP1 df -k /local/ | awk '{print \$4}' | tail -1 EOIP1 EOIP 

If the second server asks for a password, then this wont work, instead use sshpass to provide the password as

ssh user@<ip> <<EOIP sshpass -p password ssh user1@<ip1> <<EOIP1 df -k /local/ | awk '{print \$4}' | tail -1 EOIP1 EOIP 

Comments

0

Try this command with escaped quotes:

ssh user@<ip> "ssh user1@<ip1> \"df -k /local/ | awk '{print \$4}' | tail -1\"" 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.