forticlientssl-vpn_cli 在使用 expect 传递验证令牌之前结束

forticlientssl-vpn_cli end before verification token is passed using expect

我想使用 forticlient 自动登录我的 VPN。我使用 expect 命令自动传递密码。在输入正确的密码后,我收到了验证令牌,但在我将其写入控制台之前,脚本结束了。这是我的脚本:

#!/bin/bash

/usr/bin/expect << EOF
spawn /opt/forticlient-sslvpn/64bit/forticlientsslvpn_cli --server server.com:443 --vpnuser user --keepalive
expect "Password for VPN:"
send "MyPaSsWoRd\r"
expect "Would you like to connect to this server? (Y/N)"
send "Y\r"
expect "A FortiToken code is required for SSL-VPN login authentication."
expect EOF

如何从标准输入读取令牌,或者有更好的方法来解决这个问题吗?有没有办法创建一些配置文件,其中包含服务器地址、用户、密码等,并将其插入 forticlient_cli?

当你说 "the script ends" 时,我假设它会在大约 20 秒后超时

这就是我认为你问的问题:

expect "A FortiToken code is required for SSL-VPN login authentication."
send_user "Enter the token: "
gets stdin token
send -- "$token\r"
# then, you interact with the connection ...

我无法用 expect 解决我的问题,所以我使用 python3 lb pexpect。这是我的结果,效果很好:

import pexpect

child = pexpect.spawn('/opt/forticlient-sslvpn/64bit/forticlientsslvpn_cli --server server.com:443 --vpnuser user --keepalive', encoding='utf-8')
child.expect('Password for VPN:')
child.sendline('PaSsWoRd')
child.expect('Would you like to connect to this server\? \(Y\/N\)')
child.sendline('Y')

child.interact()
child.kill(1)
print('is alive:', child.isalive())