05-sep: [dhcp] inventariar y reservar IPs del ASUS
This commit is contained in:
@@ -1,6 +1,8 @@
|
||||
"""Consultas seguras al ASUS RT-AX88U PRO desde la Raspberry."""
|
||||
|
||||
import html
|
||||
import re
|
||||
import shlex
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
@@ -59,11 +61,63 @@ def clientes():
|
||||
return f"<pre>{html.escape(output)}</pre>"
|
||||
|
||||
|
||||
def _reservas_actuales(text):
|
||||
return re.findall(r"<([^>]+)>([^>]+)>>", text or "")
|
||||
|
||||
|
||||
def _inventario():
|
||||
leases = _run("cat /var/lib/misc/dnsmasq.leases")
|
||||
reservas = _reservas_actuales(_run("nvram get dhcp_staticlist"))
|
||||
by_mac = {mac.upper(): ip for mac, ip in reservas}
|
||||
devices = []
|
||||
for line in leases.splitlines():
|
||||
fields = line.split()
|
||||
if len(fields) < 4 or not fields[2].startswith("192.168.50."):
|
||||
continue
|
||||
mac, ip, name = fields[1].upper(), fields[2], fields[3]
|
||||
by_mac.setdefault(mac, ip)
|
||||
devices.append((ip, mac, name if name != "*" else "pendiente-identificar"))
|
||||
devices.sort(key=lambda item: int(item[0].rsplit(".", 1)[1]))
|
||||
return devices, by_mac
|
||||
|
||||
|
||||
def inventario():
|
||||
devices, _by_mac = _inventario()
|
||||
lines = ["IP MAC NOMBRE"]
|
||||
lines.extend(f"{ip:<18} {mac:<22} {name}" for ip, mac, name in devices)
|
||||
return f"<pre>{html.escape(chr(10).join(lines))}</pre>"
|
||||
|
||||
|
||||
def reservar_dhcp():
|
||||
devices, by_mac = _inventario()
|
||||
payload = "".join(f"<{mac}>{ip}>>" for mac, ip in by_mac.items())
|
||||
command = (
|
||||
f"nvram set dhcp_staticlist={shlex.quote(payload)}; "
|
||||
"nvram commit; service restart_dnsmasq"
|
||||
)
|
||||
result = _run(command)
|
||||
if result.startswith("Error "):
|
||||
return result
|
||||
return f"Reservas DHCP aplicadas: {len(by_mac)} dispositivos. Leases inventariados: {len(devices)}."
|
||||
|
||||
|
||||
def reservas():
|
||||
output = _run("nvram get dhcp_staticlist")
|
||||
return f"<pre>{html.escape(output)}</pre>"
|
||||
|
||||
|
||||
def reiniciar():
|
||||
return _run("reboot")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
action = sys.argv[1] if len(sys.argv) > 1 else "estado"
|
||||
actions = {"estado": estado, "clientes": clientes, "wifi": wifi}
|
||||
actions = {
|
||||
"estado": estado,
|
||||
"clientes": clientes,
|
||||
"wifi": wifi,
|
||||
"inventario": inventario,
|
||||
"reservar": reservar_dhcp,
|
||||
"reservas": reservas,
|
||||
}
|
||||
print(actions.get(action, estado)())
|
||||
|
||||
Reference in New Issue
Block a user