我需要编写一个 Python 脚本,它可以告诉我"If I am connected to VPN or NOT"进一步使用它作为Test_Case:



现在,我正在尝试编写一个python脚本,该脚本可以给出二进制结果,以检查我的计算机是连接到Corporate_VPN(Connection_Name)还是未连接到Corporate_vpn。

我尝试了很少的文章和文章,但我可以找到这些文章,但没有成功。这是一些:

我尝试了这篇文章:在Python中获得连接的VPN名称

试验:

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-netnemanager"时,我得到了这个错误:

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:Anaconda3python.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('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:UserssamolaAppDataLocalTemp1pip-record-91dmsyv1install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    creating C:UserssamolaAppDataLocalTemp1pip-install-p1feeotmdbus-pythonbuild
    creating C:UserssamolaAppDataLocalTemp1pip-install-p1feeotmdbus-pythonbuildtemp.win-amd64-3.6
    error: [WinError 193] %1 is not a valid Win32 application
    ----------------------------------------
Command "C:Anaconda3python.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('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:UserssamolaAppDataLocalTemp1pip-record-91dmsyv1install-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:UserssamolaAppDataLocalTemp1pip-install-p1feeotmdbus-python

稍后,当我尝试" pip install 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:Anaconda3python.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('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:UserssamolaAppDataLocalTemp1pip-record-7mvtqy_dinstall-record.txt --single-version-externally-managed --compile:
    running install
    running build
    creating C:UserssamolaAppDataLocalTemp1pip-install-lp5w3k60dbus-pythonbuild
    creating C:UserssamolaAppDataLocalTemp1pip-install-lp5w3k60dbus-pythonbuildtemp.win-amd64-3.6
    error: [WinError 193] %1 is not a valid Win32 application
    ----------------------------------------
Command "C:Anaconda3python.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('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record C:UserssamolaAppDataLocalTemp1pip-record-7mvtqy_dinstall-record.txt --single-version-externally-managed --compile" failed with error code 1 in C:UserssamolaAppDataLocalTemp1pip-install-lp5w3k60dbus-python

我还尝试关注帖子,也没有帮助:https://www.reddit.com/r/learnpython/comments/5qkpu1/python_script_to_check_check_check_connection_to_to_to_to_vpn_or_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

我不确定我现在在做什么。注意:我在Corporate VPN Connection中做所有这些。

pip install python-NetworManager

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

net虫子是只有Linux的应用。

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

IP地址必须是字符串:

    host = '192.168.*.*'

这是代码,它告诉您是否已连接到VPN。

注意: IPv4机器上VPN上的Adress每次连接到VPN时可能会更改。

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")

最新更新