【发布时间】:2020-07-14 16:41:30
【问题描述】:
我正在尝试编写一个脚本,它将向 vpn 提供凭据,但需要注意的是,VPN 要求我提供 OTP。
这是我的脚本:
#! /usr/bin/expect
set vpn [ lindex $argv 0]
set group [ lindex $argv 1]
set user [ lindex $argv 2]
set secret [ lindex $argv 3]
log_user 2
spawn nmcli con up $vpn --ask
expect {
"GROUP:" {
send "$group\r"
exp_continue
}
"Username:" {
send "$user\r"
exp_continue
}
Password: {
send_user "\nProvide RSA OTP:"
expect_user -re ":(.*)\r"
set otp $expect_out(0,string) <--------- Error!!!
set pass "$secret$otp"
send "$pass\r"
exp_continue
}
"Connection successfully activated" {
send_user "Connected to VPN\r"
}
"Login failed" {
send_user "Login failed\r"
exp_continue
}
"already active" {
send_user "Already connected to VPN\r"
}
}
exit
我在我认为是问题根源的地方添加了一个箭头,因为当我运行这个脚本时,它看起来像 expect_out 包含 Password:。鉴于我在手册页中读到的内容,我想象每次进行新匹配时都会清除缓冲区,因此它包含在最近的期望子句中匹配的字符串(在本例中为 expect_user -re ":(.*)\r"),但似乎我错了。我没有看到任何说明需要使用 interact 等不同功能访问 expect_user 内容的注释,所以我不确定我的密码去哪儿了?
谢谢
【问题讨论】:
-
你不想使用 1 来获取捕获组,而不是 0 来获取整个匹配吗?
-
模式中的
\r看起来也不正确。请参阅手册页中的示例。 -
提示:使用
parray命令检查 Tcl 数组的内容:parray expect_out
标签: bash automation expect