我想用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?
当你说“脚本结束”时,我认为它在大约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 lib 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())