本文主要講解如何用shell通過跳板機直接登錄目標(biāo)服務(wù)器猬错,免去在跳板機操作環(huán)節(jié)饥漫,其中涉及expect犀变、spawn妹孙、ssh,沒有安裝請自行查閱
編寫relay.exp文件
賦權(quán)限755获枝,mac推薦放到/usr/local/bin/
目錄下
需要手工設(shè)置腳本中的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
編寫調(diào)用文件
名字自定義蠢正,本文用rohn.ssh命名
vim rohn.ssh
chmod +x rohn.ssh
文件內(nèi)容:
/usr/local/bin/relay.exp 目標(biāo)服務(wù)器地址 登錄賬號 登錄密碼
cp rohn.ssh /usr/local/bin/.
在命令行執(zhí)行 rohn.ssh 即可直接登錄遠程目標(biāo)的服務(wù)器,如果想多開窗口省店,session共享嚣崭,免多次重復(fù)輸入密碼登錄,請看 http://www.reibang.com/p/ce5e01375976