繁体   English   中英

“Expect”命令行脚本连接到VPN并输入密码

[英]“Expect” command line script to connect to VPN and enter password

我正在尝试编写一个“Expect”脚本来连接到 VPN...尝试编写并期望( https://likegeeks.com/expect-command/ )脚本来连接到 vpn,这是正确的想法:

连接的命令是:

sudo vpnName [ENTER] *Password* [ENTER] Random number 1-100 [ENTER] [ENTER]

所以期望脚本会是这样的:

#!/usr/bin/expect -f 
set randNum [(( ( RANDOM % 100 )  + 1 ))]
send -- "sudo vpnName\r"
send -- "*password*\r"
send -- "randNum\r \r"

您正在尝试将 bash 表达式与 tcl 结合使用(语言期望正在使用)

改为使用:

set randNum [expr {int(rand()*100) + 1}]

在预期中,您需要spawn一个进程,然后才能与之交互:

#!/usr/bin/expect -f 
set randNum [expr {int(rand()*100) + 1}] # as per @Sorin's answer

spawn sudo vpnName

expect "assword"          # wait for the password prompt
send -- "*password*\r"

expect "whatever matches the random number prompt"
send -- "$randNum\r\r"

# this keeps the vpn process running, but returns interactive control to you:
interact

提示:在调试期望代码时,使用expect -d -f file.exp启动它——这对于查看期望模式是否与您认为的匹配非常有价值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM