简体   繁体   English

从执行 ssh 两级的 shell 脚本中获取 output

[英]Get output from a shell script that does ssh two level

I have two shell scripts like below:我有两个 shell 脚本,如下所示:

Script1:脚本1:

ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null my_username@jump_box <<EOF
ls
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null actual_host <<EOF1
sudo docker ps --format='{{json .}}'
EOF1
EOF

Script2:脚本2:

details=nothing
ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null my_username@jump_box <<EOF
ls
details=$(ssh -q -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null actual_host "sudo docker ps --format='{{json .}}'")
EOF
echo "${details}"

I need the docker details in a varilable in my local machine so that I can do some operations on it.我需要本地机器中的变量中的 docker 详细信息,以便我可以对其进行一些操作。 The first script runs fine and I can see the output of the docker command on my local machine but the second script doesn't work.第一个脚本运行良好,我可以在本地计算机上看到 docker 命令的 output,但第二个脚本不起作用。 It seems to be hung/stuck and doesn't do anything and I have to forcefully quit it.它似乎被挂起/卡住并且没有做任何事情,我必须强行退出它。

Like the comment from @ Gordon Davisson , use a jumpbox.就像@ Gordon Davisson的评论一样,使用跳箱。

But you can define it in the ~/.ssh/config file, too.但是你也可以在~/.ssh/config文件中定义它。

HOST my_jump_box
  hostname jump_box
  user my_username
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null 

HOST actual
  hostname actual_hostname
  user actual_user
  ProxyJump my_jump_box
  StrictHostKeyChecking no
  UserKnownHostsFile /dev/null 
  RemoteCommand sudo docker ps --format='{{json .}}'"

Then you can just use ssh actual然后你可以使用ssh actual

To fetch the output details=$(ssh actual) .获取 output details=$(ssh actual)

Btw.顺便提一句。 Your specific problem could also be solved by changing script2 to:您的具体问题也可以通过将 script2 更改为:

#!/bin/bash
details=$(./script1)
echo "$details"

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

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