From 591080c395c85354e3b12e8a0511dbe128eeea96 Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Tue, 5 Jul 2016 12:23:34 -0400 Subject: [PATCH 1/3] Accept either : or - as mac separator --- main.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/main.py b/main.py index 9dc664c..fe73ba1 100755 --- a/main.py +++ b/main.py @@ -219,7 +219,7 @@ def list_users(): lines = render_users_tree(tree) return render_template("users.html", users=lines) -mac_re = re.compile("(?:[0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}") +mac_re = re.compile("(?:[0-9a-fA-F]{2}[:-]){5}[0-9a-fA-F]{2}") ip_re_str = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}" ip_re = re.compile(ip_re_str) DhcpHostLine = namedtuple("DhcpHostLine","macs ip tag") @@ -324,7 +324,7 @@ def parse_macs(macs): p = p.strip() m = mac_re.match(p) if m: - r.append(m.group(0)) + r.append(m.group(0).replace("-",":")) return r def rename(src, dst): From 7577e83915ef21986710bae10bccaac2b264b861 Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Tue, 5 Jul 2016 13:02:34 -0400 Subject: [PATCH 2/3] 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 %} From 86641dcca5ec8b62a5af3f42bfd5d89ec72b16b8 Mon Sep 17 00:00:00 2001 From: Andy Teijelo Date: Tue, 5 Jul 2016 14:54:06 -0400 Subject: [PATCH 3/3] Show lease information in the IP page --- main.py | 21 +++++++++++++++++++++ static/css/dpto2.css | 5 +++++ templates/ip.html | 15 +++++++++++++++ 3 files changed, 41 insertions(+) diff --git a/main.py b/main.py index d0fe646..2ba7a98 100755 --- a/main.py +++ b/main.py @@ -438,12 +438,33 @@ def ip(last_byte): return redirect(url_for('ips')) else: ipmap = load_ips() + leases = load_leases() + lease = None + try: + if last_byte in leases: + lease = leases[last_byte] + today = time.localtime().tm_wday + expiry_t = time.localtime(int(lease[0])) + expiry_day = expiry_t.tm_wday + expiry_hour = time.strftime("%I:%M %p", expiry_t) + if today == expiry_day: + lease[0] = "las " + expiry_hour + elif (today + 1) % 7 == expiry_day: + lease[0] = "ma\N{LATIN SMALL LETTER N WITH TILDE}ana a las " + expiry_hour + else: + lease[0] = time.strftime("%B %d, ", expiry_t) + expiry_hour + except ValueError: + pass + except IndexError: + pass + addr = "{}.{}".format(ipmap["prefix"], last_byte) return render_template( "ip.html", addr=addr, valid_colors=valid_colors, meta=ipmap[last_byte], + lease=lease, ) @app.route("/logout") diff --git a/static/css/dpto2.css b/static/css/dpto2.css index 76b0af9..b732f2c 100644 --- a/static/css/dpto2.css +++ b/static/css/dpto2.css @@ -111,6 +111,11 @@ span.leased { font-size: x-small; } +div.lease { + color: #66bb6a; + margin-bottom: 5px; +} + div.ips-cell.dhcp-pool { background-color: #bed; } diff --git a/templates/ip.html b/templates/ip.html index 64c2555..44618d2 100644 --- a/templates/ip.html +++ b/templates/ip.html @@ -99,10 +99,25 @@ + + {% if lease %} +
+
+
+ + Otorgado a {{ lease[1] }} + {% if lease[3] != "*" %} ({{ lease[3] }}) {% endif %}
+ + hasta {{ lease[0] }} +
+
+
+ {% endif %} + {% if create_error %}

Error: {{ errormsg }}