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

时间:2018-09-13 21:26:57

标签: bash expect vpn

我想使用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

如何从stdin中读取令牌,或者有更好的方法来解决此问题?有没有一种方法可以创建一些配置文件,将服务器地址,用户名,密码等插入到forticlient_cli中?

1 个答案:

答案 0 :(得分:0)

我无法用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())