我想用Bash脚本连接到几个ssh服务器(如代理),如下所示:
ssh server 1 -> ssh server 2 -> ssh server 3
在此示例中,我只能通过server 2
访问server 1
。另外,我只能通过server 3
访问server 2
。
实际上我想通过一个命令(Bash脚本)轻松访问server 3
来实现它。
答案 0 :(得分:0)
此操作将由ssh config
和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
期待代码(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