37 lines
1.2 KiB
Python
37 lines
1.2 KiB
Python
#!/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())
|