简体   繁体   中英

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. 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 .

This is my first timing attempting a shell script so this is all new to me

Make sure you have passwrordless ssh set up before doing anything. You can use ssh-copy-id username@servername to do this. After that, the following command will work 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 ; the -J option tells ssh to tunnel through server1 and server2 first. The -A option should ensure that each of the intermediate servers has access to your local agent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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