简体   繁体   English

通过 3 个服务器到 SSH 的 Shell 脚本

[英]Shell script to SSH through 3 servers

As the title states I have been trying to SSH from server 1 to server 2 to server 3 using a shell script.正如标题所述,我一直在尝试使用 shell 脚本从服务器 1 到服务器 2 再到服务器 3 进行 SSH。 I have been able to get this working:我已经能够做到这一点:

eval ssh-agent -s

ssh-agent

ssh-add -k key.pem

ssh -At -I key.pem root@server1 "ssh -At server2"

I can work off the terminal in server 2 fine doing this but I do not know how to continue this to get to server 3. And once there I will need to navigate to a folder and run a command symfony cc .我可以在服务器 2 中的终端上正常工作,但我不知道如何继续此操作以到达服务器 3。一旦到了那里,我将需要导航到一个文件夹并运行命令symfony cc

This is my first timing attempting a shell script so this is all new to me这是我第一次尝试 shell 脚本,所以这对我来说是全新的

Make sure you have passwrordless ssh set up before doing anything.在做任何事情之前,请确保您设置了无密码 ssh。 You can use ssh-copy-id username@servername to do this.您可以使用ssh-copy-id username@servername来执行此操作。 After that, the following command will work ssh root@server1 \\ ssh root@server2 \\ ssh root@server3 "command"之后,以下命令将起作用ssh root@server1 \\ ssh root@server2 \\ ssh root@server3 "command"

Assuming the same key can be used for each step, it can be as simple as假设每个步骤都可以使用相同的密钥,它可以很简单

eval $(ssh-agent -s)
ssh-add -k key.pem
ssh -A -Jserver1,server2 server3

You'll simply try to connect to server3 ;您只需尝试连接到server3 the -J option tells ssh to tunnel through server1 and server2 first. -J选项告诉ssh首先通过server1server2建立隧道。 The -A option should ensure that each of the intermediate servers has access to your local agent. -A选项应确保每个中间服务器都可以访问您的本地代理。

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

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