【问题标题】:trying to connect to a openvpn config file and test my connection using python尝试连接到 openvpn 配置文件并使用 python 测试我的连接
【发布时间】:2022-01-12 18:59:39
【问题描述】:

我有很多 .ovpn openvpn 配置文件,我需要连接并测试我与它们的连接。 我已经设法使用subprocess 连接到他们

但是问题是我在运行代码的时候,Python中途执行了openvpn连接命令,在系统连接vpn之前执行了以下命令。 而当我使用subprocess.Popen().wait()时,当系统连接到vpn时,它不再关闭shell并且代码卡在那里。 我不知道如何解决。 这是我的代码:

result = {"successful": [], "failed": []}
configs = os.listdir(Path('configs')) # load config files
for config in configs:
    # connect to vpn
    subprocess.Popen(f'echo pass | sudo -S openvpn --config configs/{config} --auth-user-pass pass.txt', shell=True).wait() 
    newip = check_ip(session)
    if newip in ips:
        result["failed"].append({config: newip})
    else:
        result["successful"].append({config: newip})
        ips.append(newip)
    # killing the connection
    subprocess.Popen('echo pass | sudo -S killall openvpn', shell=True)
    print('sleeping for 30')
    time.sleep(30)

请帮忙。

【问题讨论】:

    标签: python shell subprocess connection openvpn


    【解决方案1】:

    也许这会有所帮助:

    process = subprocess.Popen(['sudo', 'openvpn', "filename"],stdout=subprocess.PIPE)
    while True:
        output = process.stdout.readline()
        if output:
            print (output.strip())
            if "Initialization Sequence Completed" in str(output):
                print("Do something if it succeed")
            else if "Failed or something":
                print("Do something if it fails")
     
    retval = process.poll()
    

    它将启动 vpn 文件,等待某种 str - 在本例中为“Initialization Sequence Completed”,然后运行一些代码。 :)

    【讨论】:

    • 此方法无效。该过程只返回一行。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-15
    • 2022-11-19
    • 2021-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多