From cb5b171018c27b2db80aa0a3fa48f62ad7c3b4a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Teijelo=20P=C3=A9rez?= Date: Thu, 5 Nov 2015 12:15:35 -0500 Subject: [PATCH] Read dhcp_hosts file --- dhcp-hosts.dpto2 | 132 ++++++++++++++++++++++++++++++++++++++++ dhcp-opts.dpto2 | 1 + main.py | 51 +++++++++++++++- static/img/unusable.png | Bin 204 -> 200 bytes templates/ip.html | 20 ++++++ templates/ips.html | 10 +-- users.dpto2 | 14 +++++ 7 files changed, 221 insertions(+), 7 deletions(-) create mode 100644 dhcp-hosts.dpto2 create mode 100644 dhcp-opts.dpto2 create mode 100644 templates/ip.html create mode 100644 users.dpto2 diff --git a/dhcp-hosts.dpto2 b/dhcp-hosts.dpto2 new file mode 100644 index 0000000..504ab12 --- /dev/null +++ b/dhcp-hosts.dpto2 @@ -0,0 +1,132 @@ +# { "reserved-by": "Andy", "locked": true } +5c:f9:ff:ff:ff:ff,00:23:ff:ff:ff:ff,10.6.122.35,set:client5cf9ffffffff + +# lara +00:e0:ff:ff:ff:ff,4c:72:ff:ff:ff:ff,10.0.0.1 +# { "reserved-by": "Andy" } +f0:e0:ff:ff:ff:ff,10.0.0.132 + +# nexus 5 +f8:a9:ff:ff:ff:ff,10.0.0.36,set:clientf8a9ffffffff + +ff:ff:ff:ff:ff:fe,10.0.0.2 +ff:ff:ff:ff:ff:fe,10.0.0.3 +ff:ff:ff:ff:ff:fe,10.0.0.4 +ff:ff:ff:ff:ff:fe,10.0.0.5 +ff:ff:ff:ff:ff:fe,10.0.0.6 +ff:ff:ff:ff:ff:fe,10.0.0.7 +ff:ff:ff:ff:ff:fe,10.0.0.8 +ff:ff:ff:ff:ff:fe,10.0.0.9 +ff:ff:ff:ff:ff:fe,10.0.0.10 +ff:ff:ff:ff:ff:fe,10.0.0.11 +ff:ff:ff:ff:ff:fe,10.0.0.12 +ff:ff:ff:ff:ff:fe,10.0.0.13 +ff:ff:ff:ff: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:ff:ff:fe,10.0.0.96 +ff:ff:ff:ff:ff:fe,10.0.0.97 +ff:ff:ff:ff:ff:fe,10.0.0.98 +ff:ff:ff:ff:ff:fe,10.0.0.99 +ff:ff:ff:ff:ff:fe,10.0.0.100 +ff:ff:ff:ff:ff:fe,10.0.0.101 +ff:ff:ff:ff:ff:fe,10.0.0.102 +ff:ff:ff:ff:ff:fe,10.0.0.103 +ff:ff:ff:ff:ff:fe,10.0.0.104 +ff:ff:ff:ff:ff:fe,10.0.0.105 +ff:ff:ff:ff:ff:fe,10.0.0.106 +ff:ff:ff:ff:ff:fe,10.0.0.107 +ff:ff:ff:ff:ff:fe,10.0.0.108 +ff:ff:ff:ff:ff:fe,10.0.0.109 +ff:ff:ff:ff:ff:fe,10.0.0.110 +ff:ff:ff:ff:ff:fe,10.0.0.111 +ff:ff:ff:ff:ff:fe,10.0.0.112 +ff:ff:ff:ff:ff:fe,10.0.0.113 +ff:ff:ff:ff:ff:fe,10.0.0.114 +ff:ff:ff:ff:ff:fe,10.0.0.115 +ff:ff:ff:ff:ff:fe,10.0.0.116 +ff:ff:ff:ff:ff:fe,10.0.0.117 +ff:ff:ff:ff:ff:fe,10.0.0.118 +ff:ff:ff:ff:ff:fe,10.0.0.119 +ff:ff:ff:ff:ff:fe,10.0.0.120 +ff:ff:ff:ff:ff:fe,10.0.0.121 +ff:ff:ff:ff:ff:fe,10.0.0.122 +ff:ff:ff:ff:ff:fe,10.0.0.123 +ff:ff:ff:ff:ff:fe,10.0.0.124 +ff:ff:ff:ff:ff:fe,10.0.0.125 +ff:ff:ff:ff:ff:fe,10.0.0.126 +ff:ff:ff:ff:ff:fe,10.0.0.127 + diff --git a/dhcp-opts.dpto2 b/dhcp-opts.dpto2 new file mode 100644 index 0000000..6fbf936 --- /dev/null +++ b/dhcp-opts.dpto2 @@ -0,0 +1 @@ +tag:client5cf9ffffffff,option:router,10.0.0.1 diff --git a/main.py b/main.py index ed44691..abd4d78 100755 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from flask import Flask, request, abort, make_response, \ render_template, redirect, url_for, \ json, jsonify, session, flash +from collections import namedtuple import hashlib import random import shutil @@ -162,9 +163,54 @@ 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}") +ip_re = re.compile("\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}") +DhcpHostLine = namedtuple("DhcpHostLine","macs ip") + +def parse_dchp_host(line): + macs = [] + ip = "" + parts = line.split(",") + for p in parts: + p = p.strip() + m = mac_re.match(p) + if m: + macs.append(m.group(0)) + m = ip_re.match(p) + if m: + ip = m.group(0) + return DhcpHostLine(macs, ip) + +Ip = namedtuple("Ip","reserved_by dhcp") + @app.route("/ips") def ips(): - return render_template("ips.html") + ipmap = {} + for i in range(1,255): + ipmap[i] = Ip("",True) + ipmap[0] = ipmap[255] = Ip("", False) + with open(dhcp_hosts_file) as f: + meta = {} + for line in f: + line = line.strip() + if line.startswith("#"): + try: + meta = json.loads(line[1:]) + except: + meta = {} + continue + if not isinstance(meta,dict): + meta = {} + continue + if re.match("^[0-9a-fA-F]{2}:",line): + r = parse_dchp_host(line) + ip = int(r.ip.split(".")[-1]) + ipmap[ip] = Ip(meta.get("reserved-by",""), False) + return render_template("ips.html", ipmap=ipmap) + +@app.route("/ip/") +def ip(addr): + return render_template("ip.html", ip=addr) @app.route("/logout") def logout(): @@ -174,6 +220,7 @@ def logout(): if __name__ == '__main__': if "debug" in sys.argv: users_file = "users.dpto2" - dhcp_hosts_file = "dhcp_hosts.dpto2" + dhcp_hosts_file = "dhcp-hosts.dpto2" + dhcp_opts_file = "dhcp-opts.dpto2" app.debug = True app.run(host="0.0.0.0") diff --git a/static/img/unusable.png b/static/img/unusable.png index 91497477f99c24cbd3f241d916cf36e9f2ea148b..86c05aa3b7db961b55d75a259336684d6deb9c6c 100644 GIT binary patch delta 111 zcmV-#0FeL80muQ6JPO4C00PAUiPnGbkwz$5MM*?KR2Y?g&A|pLD*_94eIlQwp^k_s$k=8G$vZcqje`zR(;q%Brg#lL5e}ox4f>#U Rd*1*6002ovPDHLkV1n<^EY$!2 delta 115 zcmV-(0F3|00n7oAJPP~(00R60q$i{>kwz$9Nl8ROR2Y?g&d~{gAOHl=r~WmCqqP45 z(gq>zU_Tl~<$WDI4lZFu#)88$IrdUf(C&zw`tlPYc;`awae@_Fm|#H*GaMg&5l(+w V642`Vq6q*1002ovPDHLkV1j$gE*t;= diff --git a/templates/ip.html b/templates/ip.html new file mode 100644 index 0000000..69ccad5 --- /dev/null +++ b/templates/ip.html @@ -0,0 +1,20 @@ +{% extends "base.html" %} +{% block title %}Subir Circuito{% endblock %} + +{% block head %} + {{ super() }} +{% endblock %} + +{% block content %} + +
+

AsignaciĆ³n de 10.6.122.{{ ip }}

+
+ +
+
+ +
+
+ +{% endblock %} diff --git a/templates/ips.html b/templates/ips.html index 61a73df..807cf48 100644 --- a/templates/ips.html +++ b/templates/ips.html @@ -23,17 +23,17 @@ {%- if col == 7 %} ips-ver-half{% endif %} {%- if row == 7 %} ips-hor-half{% endif %} {% if unusable %} unusable{% endif %} - {%- if 216 <= ip <= 239 %} dhcp-pool{% endif -%} + {%- if ipmap[ip].dhcp %} dhcp-pool{% endif -%} ">
- {% if 99 <= ip <= 103 or ip == 96%} - Andy - {% elif 216 <= ip <= 239 %} + {% if ipmap[ip].dhcp %} DHCP + {% else %} + {{ ipmap[ip].reserved_by }} {% endif %}
{% if usable %} - {% if ip == 14 or ip == 99 %} + {% if ipmap[ip].locked %} {% endif %} .{{ row*16 + col }} diff --git a/users.dpto2 b/users.dpto2 new file mode 100644 index 0000000..5f268ba --- /dev/null +++ b/users.dpto2 @@ -0,0 +1,14 @@ +# Sample users file, all hashes below are for the password "password" +guest Cleartext-Password := "jpqfvxat" +ateijelo NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" +apiad NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by ateijelo +pepe NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by ateijelo +yudy NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by ateijelo +dvd NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by apiad +coto NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by dvd +amy NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by dvd +lapepa NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by pepe +Bolufe NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by apiad +hydn NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" +oscarluis NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by hydn +luilver NT-Password := "8846F7EAEE8FB117AD06BDD830B7586C" # created by pepe