本文主要講解如何用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共享,免多次重復輸入密碼登錄,請看 http://www.lxweimin.com/p/ce5e01375976