【问题标题】:variables in remote nested server via ssh通过 ssh 在远程嵌套服务器中的变量
【发布时间】:2021-07-24 09:33:41
【问题描述】:

尝试在远程服务器上设置和访问一些变量 该脚本将在本地服务器上执行,它将登录到 remote-server1,然后再次登录到另一台远程服务器 remote-server2 我可以在本地和远程服务器 1 中成功设置和访问变量而没有任何问题,但在远程服务器 2 上执行相同操作时遇到问题

local1=$(echo "local-srv"); echo "${local1}"
output=$(sshpass -p "${PSSWD}" ssh -t -q -oStrictHostKeyChecking=no admin@$mgmtIP "bash -s" <<EOF
remote1=\$(echo "remote-srv1"); echo "\${remote1}"
ssh -t -q -oStrictHostKeyChecking=no "${targetCompute}" "bash -s" <<EOF2
remote2=\$(echo "remote-srv2"); echo "\${remote2}"
EOF2
EOF
)

这是我的输出

local-srv
remote-srv1

如您所见,remote-srv2 不见了

----- 更新 ---

请注意,$(echo "text") 只是为了简单起见,但这里会执行复杂的命令并将输出设置为变量

【问题讨论】:

    标签: bash shell variables ssh sh


    【解决方案1】:

    您有两个嵌套的 ssh 命令,其中包含嵌套的 here-documents,并且要延迟解释内部的 $ 表达式,您需要更多的转义。要查看问题,您可以将ssh 命令替换为cat,以查看将发送到远程计算机的内容。这是一个示例,使用您的原始代码(和一些修改后的变量定义);请注意,$&gt; 是来自我的 shell 的提示。

    $ targetCompute=remote-server2
    $ local1="local-srv"; echo "${local1}"
    local-srv
    $ cat <<EOF
    > remote1=\$(echo "remote-srv1"); echo "\${remote1}"
    > ssh -t -q -oStrictHostKeyChecking=no "${targetCompute}" "bash -s" <<EOF2
    > remote2=\$(echo "remote-srv2"); echo "\${remote2}"
    > EOF2
    > EOF
    remote1=$(echo "remote-srv1"); echo "${remote1}"
    ssh -t -q -oStrictHostKeyChecking=no "remote-server2" "bash -s" <<EOF2
    remote2=$(echo "remote-srv2"); echo "${remote2}"
    EOF2
    

    请注意,与remote1remote2 相关的行都已删除转义符,因此它们都将在remote-srv1 上扩展$ 表达式。这就是您想要的 remote1 行,但要延迟对 remote2 行的解释,您必须添加另一个转义......并且转义本身需要被转义,所以实际上每个之前都会有 three escapes $:

    $ cat <<EOF
    > remote1=\$(echo "remote-srv1"); echo "\${remote1}"
    > ssh -t -q -oStrictHostKeyChecking=no "${targetCompute}" "bash -s" <<EOF2
    > remote2=\\\$(echo "remote-srv2"); echo "\\\${remote2}"
    > EOF2
    > EOF
    remote1=$(echo "remote-srv1"); echo "${remote1}"
    ssh -t -q -oStrictHostKeyChecking=no "remote-server2" "bash -s" <<EOF2
    remote2=\$(echo "remote-srv2"); echo "\${remote2}"
    EOF2
    

    因此,本地 here-document 中的 \\\$(echo "remote-srv2")"\\\${remote2}" 变为 remote-srv1 上 here-document 中的 \$(echo "remote-srv2")"\${remote2}",然后命令实际执行,变量在 remote-srv2 上展开.

    【讨论】:

    • 感谢您的回答,$(echo "remote-srv2") 只是为了简单起见,这将执行一个复杂的命令并将输出存储在一个变量中。您的分析器是正确的,但第一部分不是必需的,也更新为问题以反映这一点
    • @electricalbah 啊,有道理;我会相应地编辑我的答案。
    【解决方案2】:

    我需要通过///逃跑

    感谢@Goron Davisson 的回答

    local1=$(echo "local-srv"); echo "${local1}"
    output=$(sshpass -p "${PSSWD}" ssh -t -q -oStrictHostKeyChecking=no admin@$mgmtIP "bash -s" <<EOF
    remote1=\$(echo "remote-srv1"); echo "\${remote1}"
    ssh -t -q -oStrictHostKeyChecking=no "${targetCompute}" "bash -s" <<EOF2
    remote2=\\\$(echo "remote-srv2"); echo "\\\${remote2}"
    EOF2
    EOF
    )
    

    输出

    local-srv
    remote-srv1
    remote-srv2
    

    【讨论】:

      猜你喜欢
      • 2015-03-07
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      • 2015-06-27
      • 1970-01-01
      • 1970-01-01
      • 2013-11-22
      • 2023-03-08
      相关资源
      最近更新 更多