所以我正在尝试编写一个脚本来简化连接到 OpenVPN 服务器的过程。
所以当我写时:
openvpn --config vpnbook-pl226-udp53.ovpn
它提示我输入用户名和密码:
Wed Apr 1 21:23:28 2020 OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4]
[EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Feb 20 2019
Wed Apr 1 21:23:28 2020 library versions: OpenSSL 1.1.1d 10 Sep 2019, LZO 2.10
Enter Auth Username:
Enter Auth Password:
在这里,我希望我的脚本自动将用户名和密码发送到 OpenVPN。
我进行了以下尝试:
尝试1:
{ echo "vpnbook"; echo "3vze4vd"; } | openvpn --config vpnbook-pl226-udp53.ovpn
尝试2:
echo -e "vpnbook\n3vze4vd" | openvpn --config vpnbook-pl226-udp53.ovpn
尝试3:
(echo $username; echo $password;) | openvpn --config vpnbook-pl226-udp53.ovpn
但它们都不起作用,我得到以下输出:
Wed Apr 1 21:38:14 2020 OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4] [EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Feb 20 2019
Wed Apr 1 21:38:14 2020 library versions: OpenSSL 1.1.1d 10 Sep 2019, LZO 2.10
然后什么也没有发生。
有人可以给我一个提示吗?
答案1
您可以将用户名和密码添加到安全文件(只有所有者才能读取的文件)。auth-user-pass
在 OpenVPN 文档中查找该通行证的详细信息 ( man openvpn
):
cat > /path/to/secret <<'X'
myusername
verysecret
X
chmod u=rw,go= /path/to/secret
现在在您的 OpenVPN 配置文件中找到该auth-user-pass
指令并像这样扩展它
auth-user-pass /path/to/secret
答案2
这看起来像是一份工作预计。
为了演示的目的,我编写了一个简短的脚本来模拟 openvpn 命令的行为:
#! /usr/bin/env bash
echo "Wed Apr 1 21:23:28 2020 OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4]"
echo "[EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Feb 20 2019"
echo "Wed Apr 1 21:23:28 2020 library versions: OpenSSL 1.1.1d 10 Sep 2019, LZO 2.10"
read -r -p 'Enter Auth Username: ' USERNAME
read -rs -p 'Enter Auth Password: ' PASSWORD
echo
echo "username was $USERNAME"
echo "password was $PASSWORD"
这是一个响应这些提示的简短的期望脚本:
#! /usr/bin/env expect
spawn ./fakevpn --config vpnbook-pl226-udp53.ovpn
expect "Enter Auth Username: " { send Hello\r } # \r is the "return" character.
expect "Enter Auth Password: " { send World\r } # It simulates hitting the Enter key.
expect eof # This ensures expect won't exit until there's no more output.
输出如下所示:
[gnubeard@mothership: ~/vpn]$ ./vpn_expect
spawn ./fakevpn --config vpnbook-pl226-udp53.ovpn
Wed Apr 1 21:23:28 2020 OpenVPN 2.4.7 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [LZ4]
[EPOLL] [PKCS11] [MH/PKTINFO] [AEAD] built on Feb 20 2019
Wed Apr 1 21:23:28 2020 library versions: OpenSSL 1.1.1d 10 Sep 2019, LZO 2.10
Enter Auth Username: Hello
Enter Auth Password:
username was Hello
password was World
编辑:出于安全原因,roiama 的解决方案优于此解决方案。我将保留这一点,以防它对试图弄清楚如何自动化交互式命令的人有帮助。