使用expect将密码发送到vpn连接

Sending password to vpn connection using expect

我尝试使用 expect 将密码发送到我的 vpn 连接,但密码发送不正确。 我使用以下脚本:

#!/usr/bin/expect -d

set pass "myPassword"
spawn snx -g -s 199.203.xxx.xxx -c ~/vpn_connection/file(IL\).p12
match_max 100000
expect "Please enter the certificate's password:"
send "$pass\r"
expect eof

我收到错误: "SNX: Connection aborted." 这意味着密码是 inncorect

虽然如果我手动输入密码到 cmd 它可以工作。 谢谢...

~ 字符在 Tcl 中并不特殊(因此 Expect)所以你的 spawn 命令应该这样写:

# '(' and ')' are not specical in Tcl.
spawn snx -g -s 199.203.xxx.xxx -c $env(HOME)/vpn_connection/file(IL).p12

# But '(' and ')' need to be quoted in shell.
spawn sh -c "snx -g -s 199.203.xxx.xxx -c ~/vpn_connection/file\(IL\).p12"
# or
spawn sh -c {snx -g -s 199.203.xxx.xxx -c ~/vpn_connection/file\(IL\).p12}