dnsmasq files written and HUP sent

This commit is contained in:
Andy Teijelo Pérez 2015-11-06 15:57:15 -05:00
parent ef67c18135
commit 69314b2c7f
5 changed files with 63 additions and 39 deletions

View file

@ -11,7 +11,7 @@ ff:ff:ff:ff:ff:fe,10.6.122.10
ff:ff:ff:ff:ff:fe,10.6.122.11 ff:ff:ff:ff:ff:fe,10.6.122.11
ff:ff:ff:ff:ff:fe,10.6.122.12 ff:ff:ff:ff:ff:fe,10.6.122.12
ff:ff:ff:ff:ff:fe,10.6.122.13 ff:ff:ff:ff:ff:fe,10.6.122.13
# { "reserved-by": "WiFi dpto2", "locked": true } # {"locked": true, "reserved-by": "WiFi dpto2"}
ff:ff:ff:ff:ff:fe,10.6.122.14 ff:ff:ff:ff:ff:fe,10.6.122.14
ff:ff:ff:ff:ff:fe,10.6.122.15 ff:ff:ff:ff:ff:fe,10.6.122.15
ff:ff:ff:ff:ff:fe,10.6.122.16 ff:ff:ff:ff:ff:fe,10.6.122.16
@ -94,14 +94,16 @@ ff:ff:ff:ff:ff:fe,10.6.122.92
ff:ff:ff:ff:ff:fe,10.6.122.93 ff:ff:ff:ff:ff:fe,10.6.122.93
ff:ff:ff:ff:ff:fe,10.6.122.94 ff:ff:ff:ff:ff:fe,10.6.122.94
ff:ff:ff:ff:ff:fe,10.6.122.95 ff:ff:ff:ff:ff:fe,10.6.122.95
# { "reserved-by": "Andy" } # {"reserved-by": "Andy"}
00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513 00:23:54:8c:65:13,10.6.122.96,set:client0023548c6513
# {"reserved-by": "Somoza"}
ff:ff:ff:ff:ff:fe,10.6.122.97 ff:ff:ff:ff:ff:fe,10.6.122.97
ff:ff:ff:ff:ff:fe,10.6.122.98 ff:ff:ff:ff:ff:fe,10.6.122.98
# {"reserved-by": "Zelda"}
ff:ff:ff:ff:ff:fe,10.6.122.99 ff:ff:ff:ff:ff:fe,10.6.122.99
ff:ff:ff:ff:ff:fe,10.6.122.100 ff:ff:ff:ff:ff:fe,10.6.122.100
# { "reserved-by": "Andy" } # {"reserved-by": "Andy"}
aa:bb:cc:dd:ee:ff,10.6.122.101,set:clientaabbccddeeff 12:24:42:23:24:45,10.6.122.101,set:client122442232445
ff:ff:ff:ff:ff:fe,10.6.122.102 ff:ff:ff:ff:ff:fe,10.6.122.102
ff:ff:ff:ff:ff:fe,10.6.122.103 ff:ff:ff:ff:ff:fe,10.6.122.103
ff:ff:ff:ff:ff:fe,10.6.122.104 ff:ff:ff:ff:ff:fe,10.6.122.104
@ -240,4 +242,3 @@ ff:ff:ff:ff:ff:fe,10.6.122.251
ff:ff:ff:ff:ff:fe,10.6.122.252 ff:ff:ff:ff:ff:fe,10.6.122.252
ff:ff:ff:ff:ff:fe,10.6.122.253 ff:ff:ff:ff:ff:fe,10.6.122.253
ff:ff:ff:ff:ff:fe,10.6.122.254 ff:ff:ff:ff:ff:fe,10.6.122.254
ff:ff:ff:ff:ff:fe,10.6.122.255

View file

@ -1,3 +1,2 @@
tag:client5cf9ffffffff,option:router,10.0.0.1 tag:client0023548c6513,option:router,10.6.122.96
tag:clientaabbccddeeff,option:router,10.6.122.96 tag:client122442232445,option:router,10.6.122.2
tag:client0023548c6513,option:router,10.6.122.2

66
main.py
View file

@ -46,7 +46,10 @@ exclusive_lock = Lock(lock_file)
shared_lock = SharedLock(lock_file) shared_lock = SharedLock(lock_file)
def reload_freeradius(): def reload_freeradius():
call("./reload_freeradius") call("./sendhup freeradius")
def reload_dnsmasq():
call("./sendhup dnsmasq")
def delete_user(deluser): def delete_user(deluser):
f = open(users_file) f = open(users_file)
@ -303,31 +306,45 @@ def parse_macs(macs):
return r return r
def write_dhcp_files(ipmap): def write_dhcp_files(ipmap):
timestamp = time.time()
tempfile = "{}.{}".format(dhcp_hosts_file, timestamp) tempfile = "{}.{}".format(dhcp_hosts_file, timestamp)
f = open(tempfile,"w") with open(tempfile,"w") as f:
prefix = ipmap["prefix"] prefix = ipmap["prefix"]
for i in range(1,255): gws = {}
ip = ipmap[i] for i in range(1,255):
if ip.dhcp_pool: ip = ipmap[i]
continue if ip.dhcp_pool:
if ip.reserved_by or ip.locked: continue
meta = OrderedDict() if ip.reserved_by or ip.locked:
if ip.reserved_by: meta = OrderedDict()
meta['reserved-by'] = ip.reserved_by if ip.reserved_by:
if ip.locked: meta['reserved-by'] = ip.reserved_by
meta['locked'] = True if ip.locked:
print("# " + json.dumps(meta)) meta['locked'] = True
if len(ip.macs) == 0: print("# " + json.dumps(meta), file=f)
macs = impossible_mac if len(ip.macs) == 0:
else: macs = impossible_mac
macs = ",".join(ip.macs) else:
tag = "" macs = ",".join(ip.macs)
if ip.gw and len(ip.macs) > 0: settag = ""
tag = ",set:client" + ip.macs[0].replace(":","").lower() if ip.gw and len(ip.macs) > 0:
print("{},{}.{}{}".format(macs,prefix,i,tag)) tag = "client" + ip.macs[0].replace(":","").lower()
f.close() settag = ",set:" + tag
gws[tag] = ip.gw
print("{},{}.{}{}".format(macs,prefix,i,settag), file=f)
shutil.move(tempfile, dhcp_hosts_file) shutil.move(tempfile, dhcp_hosts_file)
shutil.copy(dhcp_hosts_file, tempfile)
# Write custom gateways file
tempfile = "{}.{}".format(dhcp_opts_file, timestamp)
with open(tempfile,"w") as f:
for tag in gws:
print(
"tag:{},option:router,{}".format(tag,gws[tag]),
file=f,
)
shutil.move(tempfile, dhcp_opts_file)
shutil.copy(dhcp_opts_file, tempfile)
@app.route("/ip/<int:last_byte>", methods=['GET', 'POST']) @app.route("/ip/<int:last_byte>", methods=['GET', 'POST'])
@login_required @login_required
@ -347,7 +364,7 @@ def ip(last_byte):
dhcp_pool = request.form.get('dhcp-client', '') == 'pool' dhcp_pool = request.form.get('dhcp-client', '') == 'pool'
macs = parse_macs(request.form.get('dhcp-client-mac', '')) macs = parse_macs(request.form.get('dhcp-client-mac', ''))
gw = request.form.get('dhcp-gw','')[:15] gw = request.form.get('dhcp-gw','')[:15]
gw = gw if ip_re.match(gw) else '' gw = gw if ip_re.match(gw) else None
with exclusive_lock: with exclusive_lock:
ipmap = load_ips() ipmap = load_ips()
@ -360,6 +377,7 @@ def ip(last_byte):
gw=gw, gw=gw,
) )
write_dhcp_files(ipmap) write_dhcp_files(ipmap)
reload_dnsmasq()
return redirect(url_for('ips')) return redirect(url_for('ips'))
else: else:

View file

@ -1,7 +0,0 @@
#include <stdlib.h>
int main()
{
//system("/etc/init.d/freeradius reload");
system("pkill -HUP freeradius");
}

13
sendhup.cpp Normal file
View file

@ -0,0 +1,13 @@
#include <iostream>
#include <sstream>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2)
return 0;
std::ostringstream s;
s << "pkill -HUP " << argv[1];
//system("/etc/init.d/freeradius reload");
system(s.str().c_str());
}