bash 如何在脚本中使用跳转主机/跳转服务器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17372177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 05:48:14  来源:igfitidea点击:

How to use a jumphost/jump server in a script

bashsshproxydmz

提问by Classified

I need to write a script to be able to run cmds on our test servers in our secure, off-site lab. The problem is these test servers are behind a jump server/jump host. Manually, I either type:

我需要编写一个脚本,以便能够在我们安全的场外实验室中的测试服务器上运行 cmd。问题是这些测试服务器位于跳转服务器/跳转主机之后。手动,我要么输入:

bash$ ssh -A jumphost
jumphost$ ssh server1

or I edited my personal .ssh/config file to use the jump host

或者我编辑了我的个人 .ssh/config 文件以使用跳转主机

ForwardAgent yes
Host server1
ProxyCommand ssh -q jumphost nc <ip of server1> 22

to access them. How do I do this inside my script so that ssh doesn't stop/terminate at ssh -A jumphost?

访问它们。我如何在我的脚本中执行此操作,以便 ssh 不会在 ssh -A jumphost 处停止/终止?

I tried to run

我试着跑

ssh jumphost nc <ip of server1> 22 "ls /tmp"

or

或者

ssh jumphost nc <ip of server1> 22

or

或者

ssh -A jumphost nc <ip> 22

but it complains of a protocol mismatch. I don't control the jump host so I can't just leave scripts/programs at a common place for people to use. I also don't think IT will make a jump host for us to use exclusively.

但它抱怨协议不匹配。我不控制跳转主机,所以我不能把脚本/程序放在一个公共的地方供人们使用。我也不认为 IT 会成为我们专用的跳转主机。

Is there a way for me to run scripts thru a jump host? Thanks in advance for your help. If there isn't a way, I'll just tell IT they need to allow me to copy my scripts to the jump host b/c the experts on stackoverflow said it needed to be done =)

有没有办法让我通过跳转主机运行脚本?在此先感谢您的帮助。如果没有办法,我会告诉 IT 他们需要允许我将脚本复制到跳转主机 b/c,stackoverflow 上的专家说需要完成 =)

回答by twalberg

Something along the lines of:

类似的东西:

ssh -At jumpserver ssh -At server1

should work. The -tmakes sure that a pseudo-tty is allocated where needed (it may not be necessary on the second one, but I don't have a setup where I can test that at the moment, and it won't hurt). This also has the benefit that when you exit from server1, both sessions go away...

应该管用。将-t可确保在需要伪终端分配(它可能无法在第二个必要的,但我没有一个安装在那里我可以测试,在目前,它不会伤害)。这还有一个好处,当您退出时server1,两个会话都会消失......