瀏覽代碼

dnsleases pimped - output sorted by hostname + ipx

84582af
Dirk Alders 1 年之前
父節點
當前提交
79dc87c707
共有 1 個文件被更改,包括 18 次插入6 次删除
  1. 18
    6
      dnsleases

+ 18
- 6
dnsleases 查看文件

@@ -12,8 +12,13 @@ class host(object):
12 12
         self.hostname = ''
13 13
         self.lease_tm = 0
14 14
 
15
+    def sortcriteria(h):
16
+        rv = h.hostname if h.hostname != '*' else '~'
17
+        rv += ".".join(["%03d" % int(v) for v in h.ip.split('.')])
18
+        return rv.lower()
19
+
15 20
     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)
21
+        return "  * %-16s - %17s - %-10s - %15s - %s" % (self.ip, self.mac, self.state, time.asctime(time.localtime(self.lease_tm)), self.hostname)
17 22
 
18 23
 
19 24
 class hostlist(dict):
@@ -27,10 +32,19 @@ class hostlist(dict):
27 32
         else:
28 33
             self[host.mac] = host
29 34
 
35
+    def vlan_list(self, vlan):
36
+        rv = hostlist()
37
+        for h in self.values():
38
+            if h.vlan == vlan:
39
+                rv.append(h)
40
+        return rv
41
+
30 42
     def __str__(self):
43
+        entries = list(self.values())
44
+        entries.sort(key=host.sortcriteria)
31 45
         rv = ""
32
-        for key in self:
33
-            rv += str(self[key]) + '\n'
46
+        for h in entries:
47
+            rv += str(h) + '\n'
34 48
         return rv
35 49
 
36 50
 
@@ -75,7 +89,5 @@ with open('/var/lib/misc/dnsmasq.leases', 'r') as fh:
75 89
 
76 90
 for vlan in [20, 30, 40, 50, 60, 90]:
77 91
     print("Hosts in VLAN %d" % vlan)
78
-    for host in hl.values():
79
-        if host.vlan == vlan:
80
-            print("  *", host)
92
+    print(hl.vlan_list(vlan))
81 93
     print()

Loading…
取消
儲存