bundles/kea-dhcp-server: add kea-lease-list script

This commit is contained in:
Franzi 2024-01-30 21:01:20 +01:00
parent e3b63a99c2
commit a3cc5a9347
Signed by: kunsi
GPG key ID: 12E3D2136B818350
2 changed files with 41 additions and 0 deletions

View file

@ -0,0 +1,37 @@
#!/usr/bin/env python3
from csv import DictReader
from datetime import datetime, timezone
from os import scandir
from os.path import join
def parse():
NOW = datetime.now()
active_leases = {}
for file in scandir("/var/lib/kea/"):
with open(file.path) as f:
for row in DictReader(f):
expires = datetime.fromtimestamp(int(row["expire"]))
if expires >= NOW:
if (
row["address"] not in active_leases
or active_leases[row["address"]]["expires_dt"] < expires
):
row["expires_dt"] = expires
active_leases[row["address"]] = row
return active_leases.values()
def print_table(leases):
print(""" address | MAC | expires | hostname
-----------------+-------------------+---------+----------""")
for lease in sorted(leases, key=lambda r: r["address"]):
print(
f' {lease["address"]:<15} | {lease["hwaddr"].lower()} | {lease["expires_dt"]:%H:%M} | {lease["hostname"]}'
)
if __name__ == "__main__":
print_table(parse())

View file

@ -44,6 +44,10 @@ files['/etc/kea/kea-dhcp4.conf'] = {
}, },
} }
files['/usr/local/bin/kea-lease-list'] = {
'mode': '0500',
}
svc_systemd['kea-dhcp4-server'] = { svc_systemd['kea-dhcp4-server'] = {
'needs': { 'needs': {
'file:/etc/kea/kea-dhcp4.conf', 'file:/etc/kea/kea-dhcp4.conf',