Open SSH connection via SSH connection in one command

267 Views Asked by At

I might have messed the title up but I'll try to explain.

For a school project we were given a server in which we should host our project. To be able to connect to that server via SSH we need to connect to another server via SSH at first and then we can connect to the deploy via SSH.

So : Local Machine -> Intermediate Server -> Deploy Server (every "->" is an ssh connection).

My problem is that I'm going to access that deploy server very often and since remembering the username, password and the ip address of each server is a pain I wanted to create a shell script in which I type the commands, add that script to environment variables so then I can execute the script shell easily and gain access directly.

My Shell script looks something like this : sshpass -p "PASSWORD1" ssh -o StrictHostKeyChecking=no USER1@SERVER1 "sshpass -p "PASSWORD2" ssh -o StrictHostKeyChecking=no USER2@SERVER2"

Obviously this doesn't work because when you pass a command to an ssh connection it executes the command and exits immediately.

Is there any solution to this ? Also another problem : I can't install "sshpass" in Server 1 because I don't have permissions, so is there a way to type the password in the same line without installing anything ?

Thank you in advance

1

There are 1 best solutions below

0
dwenup On

I use proxypass to manage many servers through an Intermediate Server.

You don't need to creating shell script anymore!

ssh_config

# SERVER1
Host SERVER1
    HostName 1.1.1.1
    User you
#    IdentityFile /dir/test.key
    
# SERVER2
Host SERVER2
    HostName 2.2.2.2
    User you
    ProxyJump SERVER1
#    IdentityFile /dir/test.key

# SERVER3
Host SERVER3
    HostName 3.3.3.3
    User you
    ProxyJump SERVER1
#    IdentityFile /dir/test.key