本文主要讲解如何用shell通过跳板机直接登录目标服务器,免去在跳板机操作环节,其中涉及expect、spawn、ssh,没有安装请自行查阅
编写relay.exp文件
赋权限755,mac推荐放到/usr/local/bin/
目录下
需要手工设置脚本中的USER和RELAYSVR的值
#!/usr/bin/expect -f
set timeout -1
set USER rohn #账号名
set RELAYSVR "relay.rohn.com" #跳板机host
set sshhost [lindex $argv 0]
set sshuser [lindex $argv 1]
set sshpasswd [lindex $argv 2]
set sshcmd [lindex $argv 3]
spawn ssh -t $USER@$RELAYSVR
expect {
"*password:" {
stty -echo
send_user -- "\nEnter Password:"
expect_user -re "(.*)\n"
send_user "\n"
stty echo
set password $expect_out(buffer)
send -- "$password\r"
exp_continue
}
"*PASSCODE*" {
send "$password\r"
exp_continue
}
"*$" {
}
}
expect {
"*sure you want to continue connecting*" {
sleep .1
exp_send "yes\r"
exp_continue
}
"*password*" {
sleep .1
exp_send -- "$sshpasswd\r"
}
"*$" {
}
}
expect {
"*]$ " {
send "$sshcmd\r"
}
}
interact
exit
编写调用文件
名字自定义,本文用rohn.ssh命名
vim rohn.ssh
chmod +x rohn.ssh
文件内容:
/usr/local/bin/relay.exp 目标服务器地址 登录账号 登录密码
cp rohn.ssh /usr/local/bin/.
在命令行执行 rohn.ssh 即可直接登录远程目标的服务器,如果想多开窗口,session共享,免多次重复输入密码登录,请看 https://www.jianshu.com/p/ce5e01375976