-
Notifications
You must be signed in to change notification settings - Fork 1
/
indicator-ip
executable file
·95 lines (68 loc) · 2.14 KB
/
indicator-ip
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import appindicator
import gtk
import netifaces
import urllib2
URL_PUBLIC_IP = 'http://automation.whatismyip.com/n09230945.asp'
def get_public_ipv4():
"""This function return a public IPv4 address.
Returns:
str. The public IPv4 address.
"""
opener = urllib2.build_opener()
opener.addheaders = [('cache-control', 'no-cache')]
return opener.open(URL_PUBLIC_IP).read()
def get_private_ipv4(i):
"""This function return IPv4 address of the interface.
Args:
i (str): The name of the interface.
Returns:
str. The IPv4 address of the interface.
"""
return netifaces.ifaddresses(i)[netifaces.AF_INET][0]['addr']
def create_menu(indi,*args):
"""This function creates a GTK menu.
Args:
indi (appindicator.Indicator): The instance of appindicator.
"""
menu = gtk.Menu()
menu_item = gtk.MenuItem("Refresh")
menu_item.connect("activate",create_menu,ind)
menu.append(menu_item)
menu_item.show()
# IPv4 public
try:
menu_item = gtk.MenuItem("Public : %s" % get_public_ipv4())
except:
menu_item = gtk.MenuItem("No public IPv4")
finally:
menu.append(menu_item)
menu_item.show()
# IPv4 private
for i in netifaces.interfaces():
if i != "lo":
try:
menu_item = gtk.MenuItem("%s : %s" % (i, get_private_ipv4(i)))
menu.append(menu_item)
menu_item.show()
except:
continue
# IPv6
for i in netifaces.interfaces():
if i != "lo":
try:
for j in netifaces.ifaddresses(i)[netifaces.AF_INET6]:
menu_item = gtk.MenuItem("%s : %s" % (i, j['addr']))
menu.append(menu_item)
menu_item.show()
except:
continue
ind.set_menu(menu)
ind = appindicator.Indicator("indicator-network",
"/usr/share/notify-osd/icons/gnome/scalable/status/notification-network-ethernet-connected.svg",
appindicator.CATEGORY_COMMUNICATIONS)
ind.set_status (appindicator.STATUS_ACTIVE)
if __name__ == "__main__":
create_menu(ind)
gtk.main()