|
@@ -1,14 +1,81 @@
|
1
|
|
-#!/bin/bash
|
|
1
|
+#!/bin/python3
|
|
2
|
+#
|
|
3
|
+import subprocess
|
|
4
|
+import time
|
2
|
5
|
|
3
|
|
-LEASES_FILE=/var/lib/misc/dnsmasq.leases
|
|
6
|
+class host(object):
|
|
7
|
+ def __init__(self):
|
|
8
|
+ self.mac = ''
|
|
9
|
+ self.ip = ''
|
|
10
|
+ self.vlan = 0
|
|
11
|
+ self.state = ''
|
|
12
|
+ self.hostname = ''
|
|
13
|
+ self.lease_tm = 0
|
4
|
14
|
|
5
|
|
-print_vlan()
|
6
|
|
-{
|
7
|
|
- echo "Hosts in VLAN $1"
|
8
|
|
- grep 192.168.$1. $LEASES_FILE | sed 's/^/ * /'
|
9
|
|
-}
|
|
15
|
+ def __str__(self):
|
|
16
|
+ return "%-16s - %17s - %-10s - %15s - %s" % (self.ip, self.mac, self.state, time.asctime(time.localtime(self.lease_tm)), self.hostname)
|
10
|
17
|
|
11
|
|
-for vl in 20 30 40 50 60 90
|
12
|
|
-do
|
13
|
|
- print_vlan $vl
|
14
|
|
-done
|
|
18
|
+
|
|
19
|
+class hostlist(dict):
|
|
20
|
+ def __init__(self):
|
|
21
|
+ dict.__init__(self)
|
|
22
|
+
|
|
23
|
+ def append(self, host):
|
|
24
|
+ if host.mac in self:
|
|
25
|
+ if host.state < self[host.mac].state:
|
|
26
|
+ self[host.mac] = host
|
|
27
|
+ else:
|
|
28
|
+ self[host.mac] = host
|
|
29
|
+
|
|
30
|
+ def __str__(self):
|
|
31
|
+ rv = ""
|
|
32
|
+ for key in self:
|
|
33
|
+ rv += str(self[key]) + '\n'
|
|
34
|
+ return rv
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+hl = hostlist()
|
|
38
|
+
|
|
39
|
+#
|
|
40
|
+# ip n
|
|
41
|
+#
|
|
42
|
+for line in subprocess.check_output(['ip', 'n']).decode('utf-8').split('\n'):
|
|
43
|
+ data = line.split(" ")
|
|
44
|
+ h = host()
|
|
45
|
+ h.ip = data[0]
|
|
46
|
+ try:
|
|
47
|
+ h.vlan = int(h.ip.split('.')[2])
|
|
48
|
+ except IndexError:
|
|
49
|
+ pass
|
|
50
|
+ if len(data) == 6:
|
|
51
|
+ h. mac = data[4]
|
|
52
|
+ h.state = data[5]
|
|
53
|
+ else:
|
|
54
|
+ continue
|
|
55
|
+ hl.append(h)
|
|
56
|
+
|
|
57
|
+#
|
|
58
|
+# leases
|
|
59
|
+#
|
|
60
|
+with open('/var/lib/misc/dnsmasq.leases', 'r') as fh:
|
|
61
|
+ for line in fh.read().split('\n'):
|
|
62
|
+ data = line.split(' ')
|
|
63
|
+ if len(data) == 5:
|
|
64
|
+ h = hl.get(data[1], host())
|
|
65
|
+ h.lease_tm = int(data[0])
|
|
66
|
+ h.mac = data[1]
|
|
67
|
+ h.ip = data[2]
|
|
68
|
+ try:
|
|
69
|
+ h.vlan = int(h.ip.split('.')[2])
|
|
70
|
+ except IndexError:
|
|
71
|
+ pass
|
|
72
|
+ h.hostname = data[3]
|
|
73
|
+ if h.mac not in hl:
|
|
74
|
+ hl.append(h)
|
|
75
|
+
|
|
76
|
+for vlan in [20, 30, 40, 50, 60, 90]:
|
|
77
|
+ print("Hosts in VLAN %d" % vlan)
|
|
78
|
+ for host in hl.values():
|
|
79
|
+ if host.vlan == vlan:
|
|
80
|
+ print(" *", host)
|
|
81
|
+ print()
|