Gary Kelly
Gary Kelly

Reputation: 43

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

Upvotes: 0

Views: 86

Answers (2)

chepner
chepner

Reputation: 531055

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.

Upvotes: 1

Orion
Orion

Reputation: 496

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"

Upvotes: 1

Related Questions