Merge lara.matcom.uh.cu:programs/dpto2

This commit is contained in:
Andy Teijelo Pérez 2016-09-30 12:02:15 -04:00
commit bd8e339051
4 changed files with 81 additions and 3 deletions

54
main.py
View file

@ -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:
@ -219,7 +220,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")
@ -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 = []
@ -324,7 +351,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):
@ -411,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")

View file

@ -103,6 +103,18 @@ span.lock {
color: #aaa;
font-size: small;
}
span.leased {
position: absolute;
bottom: 2px;
left: 0.6em;
color: #66BB6A;
font-size: x-small;
}
div.lease {
color: #66bb6a;
margin-bottom: 5px;
}
div.ips-cell.dhcp-pool {
background-color: #bed;

View file

@ -99,10 +99,25 @@
</div>
</div>
</div>
</fieldset>
</div>
</div>
{% if lease %}
<div class="form-group">
<div class="col-xs-12">
<div class="lease">
<i class="fa fa-circle"></i>
Otorgado a {{ lease[1] }}
{% if lease[3] != "*" %} ({{ lease[3] }}) {% endif %}<br>
<i class="fa fa-circle" style="visibility: hidden;"></i>
hasta {{ lease[0] }}
</div>
</div>
</div>
{% endif %}
{% if create_error %}
<center>
<p>Error: {{ errormsg }}</p>

View file

@ -51,6 +51,9 @@
{% if ipmap[ip].locked %}
<span class="lock"><i class="fa fa-lock"></i></span>
{% endif %}
{% if not ipmap[ip].locked and ip in leases %}
<span class="leased"><i class="fa fa-circle"></i></span>
{% endif %}
<span class="ip">.{{ row*16 + col }}</span>
{% endif %}
</div>