about summary refs log tree commit diff
path: root/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
diff options
context:
space:
mode:
Diffstat (limited to '')
-rwxr-xr-xtools/dnsmasq-leases-html/dnsmasq-leases-html.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/tools/dnsmasq-leases-html/dnsmasq-leases-html.py b/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
new file mode 100755
index 0000000..c1f03db
--- /dev/null
+++ b/tools/dnsmasq-leases-html/dnsmasq-leases-html.py
@@ -0,0 +1,37 @@
+#!/usr/bin/env python3
+
+import datetime
+import ipaddress
+import os
+
+from jinja2 import Environment, FileSystemLoader
+
+
+outfile = os.getenv("DNSMASQ_LEASES_OUT", "/var/lib/dnsmasq/leases.html")
+leases_file = os.getenv("DNSMASQ_LEASES", "/var/lib/dnsmasq/dnsmasq.leases")
+
+leases = []
+
+with open(leases_file, "r") as f:
+    for line in f:
+        content = line.rstrip("\n").split(" ")
+        lease = dict()
+        if int(content[0]) == 0:
+            lease["expire"] = "never"
+        else:
+            lease["expire"] = datetime.datetime.fromtimestamp(int(content[0]))
+        lease["MAC"] = content[1]
+        lease["IP"] = ipaddress.ip_address(content[2])
+        lease["hostname"] = content[3]
+        leases.append(lease)
+
+leases = sorted(leases, key=lambda d: d["IP"])
+
+dir_path = os.path.dirname(os.path.realpath(__file__))
+templates_dir = os.path.join(dir_path, "templates")
+environment = Environment(loader=FileSystemLoader(templates_dir))
+template = environment.get_template("index.html")
+
+content = template.render(leases=leases)
+with open(outfile, "w") as fh:
+    print(content, file=fh)