About to write dhcp-hosts
This commit is contained in:
parent
65ed96cc29
commit
ef67c18135
3 changed files with 80 additions and 12 deletions
63
main.py
63
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/<int:last_byte>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def ip(last_byte):
|
||||
|
@ -302,10 +339,30 @@ 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'))
|
||||
else:
|
||||
ipmap = load_ips()
|
||||
addr = "{}.{}".format(ipmap["prefix"], last_byte)
|
||||
return render_template(
|
||||
|
|
|
@ -48,9 +48,9 @@
|
|||
<fieldset id="dhcp-fieldset" disabled>
|
||||
<div class="form-group">
|
||||
<div class="col-sm-offset-1">
|
||||
<input type="radio" id="dhcp-client-any" name="dhcp-client" value="any"
|
||||
<input type="radio" id="dhcp-client-pool" name="dhcp-client" value="pool"
|
||||
{% if meta.dhcp_pool %}checked="checked"{% endif %}/>
|
||||
<label for="dhcp-client-any">A cualquier dispositivo</label>
|
||||
<label for="dhcp-client-pool">A cualquier dispositivo</label>
|
||||
</div>
|
||||
</div>
|
||||
<div class="form-group">
|
||||
|
|
|
@ -7,6 +7,15 @@
|
|||
|
||||
{% block content %}
|
||||
|
||||
<div class="row">
|
||||
<h3>Asignaciones de IPs</h3>
|
||||
</div>
|
||||
<div class="row" style="padding-bottom: 1em;">
|
||||
<b>Red:</b> <span>10.6.122.0/24</span>
|
||||
<b>Gateway:</b> <span>10.6.122.1</span>
|
||||
<b>DNS:</b> <span>10.6.100.66, 10.6.100.67</span>
|
||||
</div>
|
||||
|
||||
<div class="row">
|
||||
{% for row in range(16) %}
|
||||
<div class="ips-row">
|
||||
|
@ -48,6 +57,8 @@
|
|||
{% endfor %}
|
||||
</div>
|
||||
|
||||
<div class="row" style="padding-bottom: 3em; padding-top: 1em;"><a href="{{ url_for('admin') }}">Regresar</a></div>
|
||||
|
||||
<script>
|
||||
$(function() {
|
||||
$(".ips-cell:not(.unusable)").hover(
|
||||
|
|
Loading…
Reference in a new issue