From ef67c181355adeddd621e9e05f84fba37326b72d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andy=20Teijelo=20P=C3=A9rez?= Date: Fri, 6 Nov 2015 15:34:58 -0500 Subject: [PATCH] About to write dhcp-hosts --- main.py | 77 ++++++++++++++++++++++++++++++++++++++++------ templates/ip.html | 4 +-- templates/ips.html | 11 +++++++ 3 files changed, 80 insertions(+), 12 deletions(-) diff --git a/main.py b/main.py index 6baf96b..d7d7829 100755 --- a/main.py +++ b/main.py @@ -3,7 +3,7 @@ from flask import Flask, request, abort, make_response, \ render_template, redirect, url_for, \ json, jsonify, session, flash -from collections import namedtuple +from collections import namedtuple, OrderedDict from subprocess import check_call, call from fcntl import flock, LOCK_EX, LOCK_SH, LOCK_UN @@ -74,7 +74,6 @@ def create_user(username, password, creator): reload_freeradius() def login_required(f): - print(f) def g(*args, **kwargs): if not session.get('logged_in', False): return redirect(url_for('login', redirect_to=request.url)) @@ -292,6 +291,44 @@ def ips(): ipmap = load_ips() return render_template("ips.html", ipmap=ipmap) +def parse_macs(macs): + r = [] + macs = macs.strip() + parts = macs.split(',') + for p in parts: + p = p.strip() + m = mac_re.match(p) + if m: + r.append(m.group(0)) + return r + +def write_dhcp_files(ipmap): + tempfile = "{}.{}".format(dhcp_hosts_file, timestamp) + f = open(tempfile,"w") + prefix = ipmap["prefix"] + for i in range(1,255): + ip = ipmap[i] + if ip.dhcp_pool: + continue + if ip.reserved_by or ip.locked: + meta = OrderedDict() + if ip.reserved_by: + meta['reserved-by'] = ip.reserved_by + if ip.locked: + meta['locked'] = True + print("# " + json.dumps(meta)) + if len(ip.macs) == 0: + macs = impossible_mac + else: + macs = ",".join(ip.macs) + tag = "" + if ip.gw and len(ip.macs) > 0: + tag = ",set:client" + ip.macs[0].replace(":","").lower() + print("{},{}.{}{}".format(macs,prefix,i,tag)) + f.close() + shutil.move(tempfile, dhcp_hosts_file) + + @app.route("/ip/", methods=['GET', 'POST']) @login_required def ip(last_byte): @@ -302,17 +339,37 @@ def ip(last_byte): # ('dhcp', 'on'), # ('dhcp-client-mac', '00:23:54:8c:65:13'), # ('dhcp-client-mac', '10.6.122.2')]) - # ImmutableMultiDict([('dhcp-client', 'any'), ('dhcp', 'on')]) + # ImmutableMultiDict([('dhcp-client', 'pool'), ('dhcp', 'on')]) # ImmutableMultiDict([('reserved', 'on'), ('reserved-by', 'Andy')]) print(request.form) + + reserved_by = request.form.get('reserved-by', '')[:100] + dhcp_pool = request.form.get('dhcp-client', '') == 'pool' + macs = parse_macs(request.form.get('dhcp-client-mac', '')) + gw = request.form.get('dhcp-gw','')[:15] + gw = gw if ip_re.match(gw) else '' + + with exclusive_lock: + ipmap = load_ips() + if not ipmap[last_byte].locked: + ipmap[last_byte] = Ip( + reserved_by=reserved_by, + dhcp_pool=dhcp_pool, + locked=False, + macs=macs, + gw=gw, + ) + write_dhcp_files(ipmap) + return redirect(url_for('ips')) - ipmap = load_ips() - addr = "{}.{}".format(ipmap["prefix"], last_byte) - return render_template( - "ip.html", - addr=addr, - meta=ipmap[last_byte], - ) + else: + ipmap = load_ips() + addr = "{}.{}".format(ipmap["prefix"], last_byte) + return render_template( + "ip.html", + addr=addr, + meta=ipmap[last_byte], + ) @app.route("/logout") def logout(): diff --git a/templates/ip.html b/templates/ip.html index 41a06c1..4bcda51 100644 --- a/templates/ip.html +++ b/templates/ip.html @@ -48,9 +48,9 @@
- - +
diff --git a/templates/ips.html b/templates/ips.html index 8bfc9f1..de06740 100644 --- a/templates/ips.html +++ b/templates/ips.html @@ -7,6 +7,15 @@ {% block content %} +
+

Asignaciones de IPs

+
+
+ Red: 10.6.122.0/24 + Gateway: 10.6.122.1 + DNS: 10.6.100.66, 10.6.100.67 +
+
{% for row in range(16) %}
@@ -48,6 +57,8 @@ {% endfor %}
+ +