-
Notifications
You must be signed in to change notification settings - Fork 8
/
vpntray.py
51 lines (39 loc) · 1.27 KB
/
vpntray.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/python
"""
Displays a Linux systray icon indicating if VPN is up or not.
Assumes VPN creates a network interface called tun0.
I wish I could have used Python 3 but apparently no pystray support for it.
Requirements: pystray, PIL
"""
import os
import pystray
import time
from PIL import Image
from pystray import Menu, MenuItem
vpnpath = "/sys/class/net/tun0/operstate"
# my preferred icons, red/green
imagedn = Image.open("/usr/share/icons/gnome/16x16/status/user-busy.png")
imageup = Image.open("/usr/share/icons/gnome/16x16/status/user-available.png")
# alternate icons, padlock
# imagedn = Image.open("/usr/share/icons/gnome/16x16/status/changes-allow.png")
# imageup = Image.open("/usr/share/icons/gnome/16x16/status/changes-prevent.png")
def loop(icon):
icon.visible = True
while icon.visible:
if os.path.isfile(vpnpath):
icon.icon = imageup
else:
icon.icon = imagedn
time.sleep(0.1)
def exit_action(icon):
icon.visible = False
icon.stop()
def no_action():
pass
icon = pystray.Icon("VPN Status Icon", imagedn)
icon.menu = Menu(
MenuItem("VPN Status Icon", no_action, enabled=False),
MenuItem("Exit", lambda : exit_action(icon), default=True)
)
icon.title = "VPN Status Icon"
icon.run(setup=loop)