我正在使用 Ubuntu 10.04 并且我用它来连接一个 pptp VPN,一切都运行正常,当我启动我的 VPN 连接时,我可以从 NetworkManager 图标中看到连接正在进行中,并且当网络图标出现小锁时,我可以看到连接已打开。
是否可以添加通知气球,就像建立网络连接时我看到的那个一样?
如果连接失败或断开,则会显示如下通知:
答案1
这更像是一种黑客攻击,但应该可以工作(对我而言可以工作)。
vpn-notify.py:
import gtk
import pynotify
import dbus
from dbus.mainloop.glib import DBusGMainLoop
def vpn_connection_handler(*args, **keywords):
state = args[0].get('State',0)
if state == 2:
n = pynotify.Notification ("VPN", "Connection established")
n.show()
pynotify.init ("icon-summary-body")
dbus_loop = DBusGMainLoop()
system_bus = dbus.SystemBus(mainloop=dbus_loop)
system_bus.add_signal_receiver(vpn_connection_handler,
dbus_interface="org.freedesktop.NetworkManager.VPN.Connection",
signal_name="PropertiesChanged")
gtk.gdk.threads_init()
gtk.main()
运行它:
python vpn-notify.py
建立 VPN 连接。
答案2
以下代码可以告诉您是否已连接到 VPN。
**Note:** *IPv4 adress of the machine on VPN might change each time you connect to 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")