0

I want to connect to several ssh server (like proxy) with Bash script like this:

ssh server 1 -> ssh server 2 -> ssh server 3

In this example I just have access to server 2 just via server 1. Also I just have access to server 3 just via server 2.

Actually I want to implement this to have easy access on server 3 via just one command (Bash script).

mortymacs
  • 3,456
  • 3
  • 27
  • 53
  • 4
    This might help: [Is it possible to combine these two ssh commands into one?](https://superuser.com/q/784052/340330) – Cyrus Aug 19 '17 at 15:40

1 Answers1

0

This action would be handled by ssh config and expect:

ssh config (my.conf):

Host Server1
    User root
    HostName 192.168.100.10
Host Server2
    User root
    HostName 192.168.100.5
Host Server3
    User root
    HostName 192.168.100.2
    ProxyJump Server1, Server2

expect code (server3.sh):

#!/bin/sh
/usr/bin/expect <<EOF
spawn ssh -F my.conf Server3
expect "password"
send "PASSWORD-OF-SERVER-1\r"
expect "password"
send "PASSWORD-OF-SERVER-2\r"
expect "password"
send "PASSWORD-OF-SERVER-3\r"
expect "$"
interact
EOF

Finally try it:

$ server3.sh
mortymacs
  • 3,456
  • 3
  • 27
  • 53