如何通过几个带有Bash脚本的ssh服务器连接到ssh服务器

时间:2017-08-19 15:35:13

标签: bash ssh

我想用Bash脚本连接到几个ssh服务器(如代理),如下所示:

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

在此示例中,我只能通过server 2访问server 1。另外,我只能通过server 3访问server 2

实际上我想通过一个命令(Bash脚本)轻松访问server 3来实现它。

1 个答案:

答案 0 :(得分:0)

此操作将由ssh configexpect处理:

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

期待代码(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

最后试一试:

$ server3.sh