我需要编写一个Python脚本,该脚本可以告诉我“是否已连接到VPN或不连接”,并将其用作Test_Case:

时间:2019-03-18 09:24:38

标签: python windows networking pip vpn

现在,我正在尝试编写一个python脚本,该脚本可能会给出二进制结果以检查我的计算机是否已连接到Corporate_VPN(连接名称)或未连接到Corporate_VPN。

我尝试了一些文章和帖子,但找不到,但没有成功。 这里是一些:

我已经尝试过此帖子:Getting Connected VPN Name in Python

并尝试:

import NetworkManager
for conn in NetworkManager.NetworkManager.ActiveConnections:
    print('Name: %s; vpn?: %s' % (conn.Id, conn.Vpn))

我收到此错误:

    ImportError                               
    Traceback (most recent call last)
    <ipython-input-6-52b1e422fff2> in <module>()
    ----> 1 import NetworkManager
          2 
          3 for conn in NetworkManager.NetworkManager.ActiveConnections:
          4     print('Name: %s; vpn?: %s' % (conn.Id, conn.Vpn))

    ImportError: No module named 'NetworkManager'

当尝试“ pip install python-NetworManager”时出现此错误:

Failed building wheel for dbus-python
  Running setup.py clean for dbus-python
Successfully built python-networkmanager
Failed to build dbus-python
Installing collected packages: dbus-python, python-networkmanager
  Running setup.py install for dbus-python ... error
    Complete output from command C:\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\samola\\AppData\\Local\\Temp\\1\\pip-install-p1feeotm\\dbus-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\samola\AppData\Local\Temp\1\pip-record-91dmsyv1\install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    creating C:\Users\samola\AppData\Local\Temp\1\pip-install-p1feeotm\dbus-python\build
    creating C:\Users\samola\AppData\Local\Temp\1\pip-install-p1feeotm\dbus-python\build\temp.win-amd64-3.6
    error: [WinError 193] %1 is not a valid Win32 application

    ----------------------------------------
Command "C:\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\samola\\AppData\\Local\\Temp\\1\\pip-install-p1feeotm\\dbus-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\samola\AppData\Local\Temp\1\pip-record-91dmsyv1\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\samola\AppData\Local\Temp\1\pip-install-p1feeotm\dbus-python\

稍后,当我尝试“点安装dbus-python”时,出现此错误:

  Failed building wheel for dbus-python
  Running setup.py clean for dbus-python
Failed to build dbus-python
Installing collected packages: dbus-python
  Running setup.py install for dbus-python ... error
    Complete output from command C:\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\samola\\AppData\\Local\\Temp\\1\\pip-install-lp5w3k60\\dbus-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\samola\AppData\Local\Temp\1\pip-record-7mvtqy_d\install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    creating C:\Users\samola\AppData\Local\Temp\1\pip-install-lp5w3k60\dbus-python\build
    creating C:\Users\samola\AppData\Local\Temp\1\pip-install-lp5w3k60\dbus-python\build\temp.win-amd64-3.6
    error: [WinError 193] %1 is not a valid Win32 application

    ----------------------------------------
Command "C:\Anaconda3\python.exe -u -c "import setuptools, tokenize;__file__='C:\\Users\\samola\\AppData\\Local\\Temp\\1\\pip-install-lp5w3k60\\dbus-python\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:\Users\samola\AppData\Local\Temp\1\pip-record-7mvtqy_d\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:\Users\samola\AppData\Local\Temp\1\pip-install-lp5w3k60\dbus-python\

在没有帮助的情况下,我也尝试过POST:https://www.reddit.com/r/learnpython/comments/5qkpu1/python_script_to_check_if_connected_to_vpn_or_not/

host = *******
ping = subprocess.Popen(["ping.exe","-n","1","-w","1",host],stdout = subprocess.PIPE).communicate()[0]
if ('unreachable' in str(ping)) or ('timed' in str(ping)) or ('failure' in str(ping)):
    ping_chk = 0
else:
    ping_chk = 1 

 if ping_chk == 1:
     print ("VPN Connected")
 else:
    print ("VPN Not Connected")

向我抛出错误:

File "<ipython-input-5-6f992511172f>", line 1
    host = 192.168.*.*
                   ^
SyntaxError: invalid syntax

我不确定我现在在做什么错。 注意:我正在公司VPN连接中完成所有操作。

2 个答案:

答案 0 :(得分:0)

pip install python-NetworManager
  

错误:[WinError 193]%1不是有效的Win32应用程序

NetworManager是Linux-only应用程序。

    host = 192.168.*.*
                   ^
SyntaxError: invalid syntax

IP地址必须为字符串:

    host = '192.168.*.*'

答案 1 :(得分:0)

这是告诉您是否已连接VPN的代码。

注意: 每次连接到VPN时,VPN上计算机的IPv4地址可能会更改。

import subprocess
host = '**.***.***.***' 
#IPv4 should be string ''
#IPv4 Address (while connected to VPN in command prompt type: ipconfig",
copy IPv4 Address digits and paste as "host = ", 
#IPv4 Address. changes each time we freshly connect to VPN. )
ping = subprocess.Popen(["ping.exe","-n","1","-w","1",host],stdout = subprocess.PIPE).communicate()[0]
if ('unreachable' in str(ping)) or ('timed' in str(ping)) or ('failure' in str(ping)):
    ping_chk = 0
else:
    ping_chk = 1 

if ping_chk == 1:
     print ("VPN Connected")
else:
    print ("VPN Not Connected")