#!/usr/bin/expect
# relay用户名
set USER [lindex $argv 0]
# relay PIN码
set PIN [lindex $argv 1]
# [可选]开发机地址, 如:user@host
set HOST [lindex $argv 2]
puts $HOST
# [可选]开发机密码
set PASSWORD [lindex $argv 3]
# 登录relay
spawn ssh $USER@relay01.duxiaoman-int.com
# 打开调试模式
#exp_internal 1
# relay PIN码 + Token(Token需手动输入)
interact
expect {
-re "password:*" {
send_user "PIN:****** + Token:"
expect_user {
-timeout -1
-re "(.*)\n"
}
set TOKEN $expect_out(buffer)
send "$PIN$TOKEN\r"
exp_continue
}
# 自动登录开发机
-re "" {
if { "$HOST" != "" } {
send "$HOST\r"
# [可选]自动开发机输入密码
if { "$PASSWORD" != "" } {
expect -re "password:" { send "$PASSWORD\r" }
}
}
}
}
# 关闭调试模式
#exp_internal 0
interact
exit
- 首先安装 expect
- 将上面的脚本copy至你的电脑上
- expect .relay.expect username ping hostname