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
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.
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.
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.
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.
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\"
"
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With