Nested ssh variable and operator expansion

Variable and operator expansion can vary depending on the number of nested ssh commands.

tgs:[ fsl_yui ] [fsl@ptj87rgd src]$ ssh vboxt@host1 ssh root@host2 ssh root@host3 ‘echo $HOSTNAME’
host1
tgs:[ fsl_yui ] [fsl@ptj87rgd src]$ ssh vboxt@host1 ssh root@host2 ssh root@host3 ‘echo \$HOSTNAME’
host2
tgs:[ fsl_yui ] [fsl@ptj87rgd src]$ ssh vboxt@host1 ssh root@host2 ssh root@host3 ‘echo \\$HOSTNAME’
host1
tgs:[ fsl_yui ] [fsl@ptj87rgd src]$ ssh vboxt@host1 ssh root@host2 ssh root@host3 ‘echo \\\$HOSTNAME’
host3.dns.localdomain

———————————————————-

To execute some command and redirect the output to the last server of the chain, redirection operators also have to be escaped:

tgs:[ fsl_yui ] [fsl@ptj87rgd src]$ ssh vboxt@host1 ssh root@host2 ssh root@host3 echo ‘echo \\\$HOSTNAME \\\> /tmp/miau’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s