我创建了一个 Python 脚本,它执行几个 REST API 调用来获取一个 cookie,然后通过管道将一个密码用于openconnect
应用程序echo "my cookie value"
的openconnect command
. 目的基本上是连接到企业 VPN。
它过去工作得很好,除了现在要输入另一个输入,似乎有 2 个网关服务器。其中一个需要手动选择并传递给提示符。
openconnect
考虑到 REST API 调用已经在获取需要传递给openconnect
提示符的 cookie,我特意只将执行调用的 Python 部分放在下面:
# [...] lots of REST API calls to fetch the portal_userauthcookie:
# for the sake of simplicity let's say it has a dummy value
portal_userauthcookie = "blahblah"
print("portal-userauthcookie = \"{}\"".format(portal_userauthcookie))
# Call openconnect with the cookie / password we got above
cmd = "echo \"{}\"".format(portal_userauthcookie)
cmd += " |"
cmd += " openconnect"
cmd += " --protocol=gp"
cmd += " --usergroup portal:portal-userauthcookie"
cmd += " --user={}".format(username)
cmd += " {}".format(vpn_portal_url)
process = subprocess.Popen(cmd, shell=True, stdin=sys.stdin, stdout=sys.stdout, stderr=sys.stderr)
out, err = process.communicate()
脚本运行时:
$ sudo ./bin/python3 ./main.py
[sudo] password for perret:
My username: my-username
My password:
My non-expired smartphone app code: my-code
portal-userauthcookie = "blahblah"
POST another-corporate.url.com&clientVer=4100&clientos=Linux
Connected to [corporate-ip]:443
SSL negotiation with [corporate-url]
Connected to HTTPS on [corporate-url]
SAML login is required via POST to this URL:
<html>
<!-- corporate html -->
</html>
Enter login credentials
portal-userauthcookie:
POST corporate.url.com
2 gateway servers available:
gateway1 (blahblah-url-1)
gateway2 (blahblah-url-2)
Please select GlobalProtect gateway.
GATEWAY: [gateway1|gateway2]:fgets (stdin): Resource temporarily unavailable
如何gateway1
在 popen 命令的提示下自动输入?
我尝试添加另一个echo
,但似乎只有一个可以工作(我已经用于传递充当密码的 cookie)。