用于检查openvpn服务器是否处于活动状态的Python脚本

2024-06-03 13:20:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在看python程序,它将检查远程openvpn服务器列表是否真的工作!! 我尝试了以下代码,但没有成功。连接vpn服务器后,除非按下^{,否则控件不会传回执行下一行。如何检查以下内容:

  • 如果openvpn正在连接服务器,则打印活动消息
  • 否则打印非活动消息
  • 整个过程都在循环中,我想在虚拟环境中运行

Code is given below

    path = tempfile.mkstemp()
    print('Path:',path)
    f = open(path, 'w')
    f.write(base64.b64decode(winner[-1]).decode('utf-8'))

    f.close()
    x = subprocess.Popen(['sudo', 'openvpn', '--config', path])
    print("\n\n Pid:",x.pid)
    try:
        print('############ inside try block############', x.pid)
        while True:
            print('############ inside while try block############', x.pid)
            time.sleep(60)

            if (x.wait() == 0):
                # died_serv_list.append(winner)
                print('\nDead server found....Exiting.....\n')
                return 
            print('############terminating the current prosses############', x.pid)
            x.send_signal(9)
            # y = subprocess.Popen(['sudo', 'kill', '-9', x.pid])
            break
        # termination with Ctrl+C
    except: #here issue only works with ctrl+C
        try:
            print('\n\n\n Nice server found...\n\n')
            good_serv_list.append(winner)
            print('we are inside exept.')
            x.kill()
        except:
            pass
        while x.poll() != 0:
            print('\n\n Donot know what the hell is this')
            time.sleep(1)
        print('\nVPN terminated')

Tags: path服务器消息issudoblockpidsubprocess