现在,我正在尝试编写一个python脚本,它可以给出一个二进制结果来检查我的机器是否连接到Corporate_VPN (Connection_Name)或没有连接到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\后来,当我尝试"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:\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\我也尝试了下面的帖子,但没有任何帮助: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连接中执行所有这些操作。
发布于 2019-03-18 21:05:25
pip install python-NetworManager错误: WinError 193 %1不是有效的Win32应用程序
NetworManager是Linux-only应用程序。
host = 192.168.*.*
^
SyntaxError: invalid syntaxIP地址必须是字符串:
host = '192.168.*.*'发布于 2019-03-18 23:32:09
这是告诉你是否连接到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")https://stackoverflow.com/questions/55218096
复制相似问题