Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ssh and environment variables remote and local

Tags:

bash

ssh

I need to use a remote variable and a local variable in the same remote ssh command

export CASSANDRA_DIR=/opt/cassandra

ssh root@sdi-prod-02 <<\EOF
  export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}')
  echo "listen_address: $READ" to directory "$CASSANDRA_DIR"
EOF

The $READ variable is working just fine while the CASSANDRA_DIR is not working. The following does work for CASSANDRA_DIR

ssh root@sdi-prod-02 echo "directory=$CASSANDRA_DIR"

thanks, Dean

like image 214
Dean Hiller Avatar asked Jul 10 '13 18:07

Dean Hiller


People also ask

How do you SSH into the environment?

This can be found by pressing the windows key and typing command prompt and hitting enter. The ssh syntax is ssh username@destation and then hitting enter and supplying your password. In this case your username will be your hawkID and your password.

Is SSH a remote connection?

What is SSH? Secure Shell, sometimes referred to as Secure Socket Shell, is a protocol which allows you to connect securely to a remote computer or a server by using a text-based interface.

What is the difference between a local variable and an environmental variable?

The global environment variables are visible from a shell session and any child processes that the shell spawns. While local variables can be available in the shell only in which they are created. System environment variables use all uppercase letters to distinguish them from normal user environment variables.


3 Answers

What should be expanded locally, keep the sigil $ as is, like $foobar

What you want to be expanded remotely, you may use backslashes : \$foobar

By default in here-docs, the variable are expanded.

Ex. :

cat<< EOF
$UID
EOF

To avoid expanding in here-doc, you can use this form :

cat<< 'EOF'
$variable_that_will_not_been_expanded
EOF

or yours :

cat<< \EOF
$variable_that_will_not_been_expanded
EOF

both works.

like image 108
Gilles Quenot Avatar answered Oct 25 '22 18:10

Gilles Quenot


If you didn't want to use a here-doc you can do it this way:

export CASSANDRA_DIR=/opt/cassandra

ssh root@sdi-prod-02 "
   export READ=\$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= \'{print \$2}\')
   echo \"listen_address: \$READ\" to directory \"$CASSANDRA_DIR\"
"
like image 39
hax0r_n_code Avatar answered Oct 25 '22 16:10

hax0r_n_code


My final result is thus which works great (notice I cahnge \EOF to EOF instead!!!!! and then escape the remote variables

export CASSANDRA_DIR=/opt/cassandra

ssh root@sdi-prod-02 <<EOF
  export READ=$(grep IPADDR /etc/sysconfig/network-scripts/ifcfg-eth0 |awk -F= '{print $2}')
  echo "listen_address: \$READ to directory $CASSANDRA_DIR"
EOF

IT all works great in that READ is generated remotely and CASSANDRA_DIR is the var on my original machine.

Dean

like image 27
Dean Hiller Avatar answered Oct 25 '22 18:10

Dean Hiller