简体   繁体   English

ssh和环境变量远程和本地

[英]ssh and environment variables remote and local

I need to use a remote variable and a local variable in the same remote ssh command 我需要在同一个远程ssh命令中使用远程变量和局部变量

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. $ SC变量工作得很好,而CASSANDRA_DIR不工作。 The following does work for CASSANDRA_DIR 以下适用于CASSANDRA_DIR

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

thanks, Dean 谢谢,迪恩

What should be expanded locally, keep the sigil $ as is, like $foobar 什么应该在本地扩展,保持sigil $原样,像$foobar

What you want to be expanded remotely, you may use backslashes : \\$foobar 你想远程扩展什么,你可以使用反斜杠: \\$foobar

By default in here-docs , the variable are expanded. 默认情况下, here-docs会扩展变量。

Ex. 防爆。 :

cat<< EOF
$UID
EOF

To avoid expanding in here-doc , you can use this form : 为避免在here-doc中扩展,您可以使用以下格式:

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: 如果您不想使用here-doc,可以这样做:

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 因此,我的最终结果非常有效(请注意我将EOF改为EOF而不是!!!!!然后转义远程变量

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. 一切都很好,因为READ是远程生成的,而CASSANDRA_DIR是我原始机器上的var。

Dean 院长

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM