【问题标题】:How to connect to a ssh server through several ssh servers with Bash script [duplicate]如何使用 Bash 脚本通过多个 ssh 服务器连接到 ssh 服务器 [重复]
【发布时间】:2018-01-28 03:00:47
【问题描述】:

我想使用这样的 Bash 脚本连接到多个 ssh 服务器(如代理):

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

在这个例子中,我只能通过server 1 访问server 2。我也只能通过server 2 访问server 3

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

【问题讨论】:

标签: bash ssh


【解决方案1】:

此操作将由ssh configexpect 处理:

ssh 配置(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

【讨论】:

    猜你喜欢
    • 2015-09-04
    • 1970-01-01
    • 2018-09-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    • 1970-01-01
    相关资源
    最近更新 更多