python - 使用 STDOUT 作为 openvpn auth-user-pass 的输入

标签 python linux bash scripting openvpn

我正在编写一个 bash 脚本来自动连接到 VPNBook 的免费 openvpn 服务。我通过调用 python 脚本收集用户名和密码,该脚本当前仅将它们打印到 STDOUT。

收集用户名和密码的python脚本是:

#!/bin/python

# title:                       vpnbook-user-pass©                    
# description:                 Gather vpnbooks username - password combinantion
# author:                      jack herer                    
# date:                        Tuesday 06 October 2015 @ 10:29:14 am
# version:                     v1.0                          
# usage:                       python vpnbook-user-pass                    
# notes:                       ~                             
# bash version:                4.3.30(1)-release             
#========================================                               
# Copyright © | jack herer | 2015 
#========================================

from bs4 import BeautifulSoup
import requests

response = requests.get('http://vpnbook.com/freevpn')
soup = BeautifulSoup(response.text, 'html.parser')
pricing = soup.find(id = 'pricing')
first_column = pricing.find('div', {'class': 'one-third'})
for li in first_column.find('ul', {'class': 'disc'}):
    if 'username' in str(li).lower():
        username = li.find('strong').text
print('The username and password combinantion is:')
print(username)

response = requests.get('http://vpnbook.com/freevpn')
soup = BeautifulSoup(response.text, 'html.parser')
pricing = soup.find(id = 'pricing')
first_column = pricing.find('div', {'class': 'one-third'})
for li in first_column.find('ul', {'class': 'disc'}):
    if 'password' in str(li).lower():
        password = li.find('strong').text
print(password)  

python 脚本的 STDOUT 是:

The username and password combinantion is:
vpnbook  
JE5Raphu  

到目前为止的 bash 脚本:

# !/bin/bash - 

# title:                       auto-vpn©                    
# description:                 Automatically connect to vpnbook's free vpn service
# author:                      jack herer                    
# date:                        Thursday 08 October 2015 @ 11:49:21 am
# version:                     v1.0                          
# usage:                       ./auto-vpn                    
# notes:                       ~                             
# bash version:                4.3.30(1)-release             
#========================================                               
# Copyright © | jack herer | 2015 
#========================================

userpass=$(python ~/vpnbook-user-pass)
echo "${userpass} "
cd $HOME/vpnbook/
openvpn --config vpnbook-euro1-tcp443.ovpn

最后一个命令 openvpn --config vpnbook-euro1-tcp443.ovpn 然后运行并要求我输入用户名和密码,如下所示:

Thu Oct  8 13:20:28 2015 OpenVPN 2.3.4 i586-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Dec  1 2014
Thu Oct  8 13:20:28 2015 library versions: OpenSSL 1.0.1k 8 Jan 2015, LZO 2.08
Enter Auth Username: 
Enter Auth Password:  

如何让我的脚本输入通过 python 脚本收集的用户名和密码?

我不介意这是 bash 还是 python 脚本所有选项都打开,但必须是 bash 或 python。

最佳答案

查看 --auth-user-pass 选项 ( here )。

对您来说,最简单的方法可能是从您的 python 脚本中删除带有 "The username and password combinantion is:" 的行,然后从 bash 启动 openvpn 这样(对我有用):

openvpn --config vpnbook-euro1-tcp443.ovpn --auth-user-pass <( python ~/vpnbook-user-pass )

一般来说,是这样的:

openvpn --config vpnbook-euro1-tcp443.ovpn --auth-user-pass <( echo -e "${userName}\n${password}" )

应该同样有效(对我有效)。


或者,您可以将凭据保存到一个文件中(确保它不是全局可读的)并将此文件作为参数传递到 --auth-user-pass 之后。

参见 this question .

编辑>

确认这对我有效:

#!/usr/bin/python

from bs4 import BeautifulSoup
import requests

response = requests.get('http://vpnbook.com/freevpn')
soup = BeautifulSoup(response.text, 'html.parser')
pricing = soup.find(id = 'pricing')
first_column = pricing.find('div', {'class': 'one-third'})
for li in first_column.find('ul', {'class': 'disc'}):
    if 'username' in str(li).lower():
        username = li.find('strong').text
    if 'password' in str(li).lower():
        password = li.find('strong').text
print(username)
print(password)

在 bash 中使用:

openvpn --config vpnbook-euro1-tcp443.ovpn --auth-user-pass <( ./fvpn3.py )

openvpn --version 的输出:

OpenVPN 2.3.4 x86_64-pc-linux-gnu [SSL (OpenSSL)] [LZO] [EPOLL] [PKCS11] [MH] [IPv6] built on Dec 1 2014
library versions: OpenSSL 1.0.1k 8 Jan 2015, LZO 2.08
Originally developed by James Yonan
Copyright (C) 2002-2010 OpenVPN Technologies, Inc.
Compile time defines: enable_crypto=yes enable_debug=yes enable_def_auth=yes enable_dependency_tracking=no enable_dlopen=unknown enable_dlopen_self=unknown enable_dlopen_self_static=unknown enable_fast_install=yes enable_fragment=yes enable_http_proxy=yes enable_iproute2=yes enable_libtool_lock=yes enable_lzo=yes enable_lzo_stub=no enable_maintainer_mode=no enable_management=yes enable_multi=yes enable_multihome=yes enable_pam_dlopen=no enable_password_save=yes enable_pedantic=no enable_pf=yes enable_pkcs11=yes enable_plugin_auth_pam=yes enable_plugin_down_root=yes enable_plugins=yes enable_port_share=yes enable_selinux=no enable_server=yes enable_shared=yes enable_shared_with_static_runtimes=no enable_small=no enable_socks=yes enable_ssl=yes enable_static=yes enable_strict=no enable_strict_options=no enable_systemd=yes enable_win32_dll=yes enable_x509_alt_username=yes with_crypto_library=openssl with_gnu_ld=yes with_ifconfig_path=/sbin/ifconfig with_iproute_path=/sbin/ip with_mem_check=no with_plugindir='${prefix}/lib/openvpn' with_route_path=/sbin/route with_sysroot=no
git revision: refs/heads/jessie/b35ad09bfc4a26e7

(请注意enable_password_save=yes)

bash --version 的输出:

GNU bash, version 4.3.30(1)-release (x86_64-pc-linux-gnu)
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later http://gnu.org/licenses/gpl.html

祝你好运!

关于python - 使用 STDOUT 作为 openvpn auth-user-pass 的输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33015777/

相关文章:

python - 从 QT Creator 转换对话框并将其合并到我的主窗口中的更好方法

python - 逆位运算符

php - 在 whm/cpanel 上禁用安全规则

bash - 带有打开编辑器的命令的 xargs 使 shell 处于奇怪的状态

linux - 如何使用深度级别 2 或 maxdepth 作为下面我的 bash 脚本的输出文本文件

python - 如何使用 Python 将 .dat 文件的内容转换为人类可读的形式?

python - 如何正确使用 tft.compute_and_apply_vocabulary 和 tft.tfidf?

linux - Linux 上 Qt Creator 中的 UTF-8

linux - 如何将远程文件 CURL 到远程服务器

bash - 转换今天的 HH :MM am/pm to epoch in Bash