aboutsummaryrefslogtreecommitdiff
path: root/packages/dnsmasq-leases-html/dnsmasq-leases-html.py
diff options
context:
space:
mode:
authorFranck Cuny <franck@fcuny.net>2024-03-06 06:29:24 -0800
committerFranck Cuny <franck@fcuny.net>2024-03-06 06:29:24 -0800
commit1e4a5aa09c1c8f43722c9c260f011398799a8e8f (patch)
treecd73e0fb8ba53bd21cee6ccf2dcc85639bbbb93f /packages/dnsmasq-leases-html/dnsmasq-leases-html.py
parentset correct git email in the profiles (diff)
downloadinfra-1e4a5aa09c1c8f43722c9c260f011398799a8e8f.tar.gz
rename `tools` to `packages` to follow convention
The convention is to use `pkgs` or `packages` for overlays and definition of custom packages. Since I'm already using `pkg` for go, I prefer to use `packages` for my scripts.
Diffstat (limited to 'packages/dnsmasq-leases-html/dnsmasq-leases-html.py')
-rwxr-xr-xpackages/dnsmasq-leases-html/dnsmasq-leases-html.py37
1 files changed, 37 insertions, 0 deletions
diff --git a/packages/dnsmasq-leases-html/dnsmasq-leases-html.py b/packages/dnsmasq-leases-html/dnsmasq-leases-html.py
new file mode 100755
index 0000000..c1f03db
--- /dev/null
+++ b/packages/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)