"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]
所以 expect 脚本应该是这样的:
#!/usr/bin/expect -f
set randNum [(( ( RANDOM % 100 ) + 1 ))]
send -- "sudo vpnName\r"
send -- "*password*\r"
send -- "randNum\r \r"
您正在尝试将 bash 表达式与 tcl(正在使用 expect 语言)组合
改用:
set randNum [expr {int(rand()*100) + 1}]
在 expect 中,您需要 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 代码时,使用 expect -d -f file.exp
启动它——这对于查看您的 expect 模式是否与您认为的匹配是非常有价值的。
我正在尝试编写一个“Expect”脚本来连接到 VPN... 尝试编写并期望 (https://likegeeks.com/expect-command/) 脚本连接到 vpn,这是正确的想法吗:
连接命令是:
sudo vpnName [ENTER] *Password* [ENTER] Random number 1-100 [ENTER] [ENTER]
所以 expect 脚本应该是这样的:
#!/usr/bin/expect -f
set randNum [(( ( RANDOM % 100 ) + 1 ))]
send -- "sudo vpnName\r"
send -- "*password*\r"
send -- "randNum\r \r"
您正在尝试将 bash 表达式与 tcl(正在使用 expect 语言)组合
改用:
set randNum [expr {int(rand()*100) + 1}]
在 expect 中,您需要 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 代码时,使用 expect -d -f file.exp
启动它——这对于查看您的 expect 模式是否与您认为的匹配是非常有价值的。