首页 > 解决方案 > I need to write a Python Script which could tell me "If I am connected to VPN or NOT" further using it as Test_Case:

问题描述

Right now I am trying to write a python script which could give a binary result to check if my machine is connected to Corporate_VPN (Connection_Name) OR Not connected to Corporate_VPN.

I have tried few articles and post which I could find but with no success. Here are some:

I have tried this post: Getting Connected VPN Name in Python

And tried:

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

I am getting this error:

    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'

When tried to "pip install python-NetworManager" I got this error:

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\

Later when I tried to "pip install dbus-python" i got this error:

  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\

I have also tried following POST as well with no help: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")

Throwing me error:

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

I am not sure what wrong I am doing right now. Note: I am doing all this in Corporate VPN connection.

标签: pythonwindowsnetworkingpipvpn

解决方案


这是告诉您是否连接到 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")

推荐阅读