From 7577e83915ef21986710bae10bccaac2b264b861 Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Tue, 5 Jul 2016 13:02:34 -0400 Subject: [PATCH] Show leased ips in the ips page --- main.py | 29 ++++++++++++++++++++++++++++- static/css/dpto2.css | 7 +++++++ templates/ips.html | 3 +++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index fe73ba1..d0fe646 100755 --- a/main.py +++ b/main.py @@ -23,6 +23,7 @@ app.secret_key = "6ab77f3c45447429c2ae163c260a626029519a66450e474c" users_file = "/etc/freeradius/dpto2/users" dhcp_hosts_file = "/etc/dnsmasq.d/dpto2/dhcp-hosts" dhcp_opts_file = "/etc/dnsmasq.d/dpto2/dhcp-opts" +dnsmasq_leases = "/var/lib/misc/dnsmasq.leases" impossible_mac = "ff:ff:ff:ff:ff:fe" class Lock: @@ -310,11 +311,37 @@ def load_ips(): ipmap["prefix"] = prefix return ipmap +def load_leases(): + leases = {} + with open(dnsmasq_leases) as f: + for line in f: + parts = line.strip().split() + try: + ip = int(parts[2].split(".")[-1]) + except IndexError: + continue + except ValueError: + continue + try: + lease = [ + int(parts[0]), # expiration timestamp + parts[1], # client mac address + parts[2], # leased ip + parts[3], # host name or * if no name sent + parts[4], # client id or * if no id sent + ] + except IndexError: + # line is shorter than I thought, skippping + continue + leases[ip] = lease + return leases + @app.route("/ips") @login_required def ips(): ipmap = load_ips() - return render_template("ips.html", ipmap=ipmap) + leases = load_leases() + return render_template("ips.html", ipmap=ipmap, leases=leases) def parse_macs(macs): r = [] diff --git a/static/css/dpto2.css b/static/css/dpto2.css index 464a308..76b0af9 100644 --- a/static/css/dpto2.css +++ b/static/css/dpto2.css @@ -103,6 +103,13 @@ span.lock { color: #aaa; font-size: small; } +span.leased { + position: absolute; + bottom: 2px; + left: 0.6em; + color: #66BB6A; + font-size: x-small; +} div.ips-cell.dhcp-pool { background-color: #bed; diff --git a/templates/ips.html b/templates/ips.html index e7836aa..57da517 100644 --- a/templates/ips.html +++ b/templates/ips.html @@ -51,6 +51,9 @@ {% if ipmap[ip].locked %} {% endif %} + {% if not ipmap[ip].locked and ip in leases %} + + {% endif %} .{{ row*16 + col }} {% endif %}