05-sep: [router] integrar ASUS en Telegram
This commit is contained in:
69
vigilancia/router_control.py
Normal file
69
vigilancia/router_control.py
Normal file
@@ -0,0 +1,69 @@
|
||||
"""Consultas seguras al ASUS RT-AX88U PRO desde la Raspberry."""
|
||||
|
||||
import html
|
||||
import subprocess
|
||||
import sys
|
||||
|
||||
|
||||
ROUTER = "jjminguez@192.168.50.1"
|
||||
KEY = "/home/pi/.ssh/router_control"
|
||||
SSH_BASE = [
|
||||
"ssh",
|
||||
"-i", KEY,
|
||||
"-o", "BatchMode=yes",
|
||||
"-o", "ConnectTimeout=8",
|
||||
ROUTER,
|
||||
]
|
||||
|
||||
|
||||
def _run(command):
|
||||
try:
|
||||
result = subprocess.run(
|
||||
SSH_BASE + [command],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=15,
|
||||
)
|
||||
except Exception as exc:
|
||||
return f"Error conectando al router: {exc}"
|
||||
if result.returncode != 0:
|
||||
return f"Error del router: {result.stderr.strip()[-300:]}"
|
||||
return result.stdout.strip()
|
||||
|
||||
|
||||
def estado():
|
||||
output = _run(
|
||||
"printf '%s\\n' "
|
||||
"'Modelo='$(nvram get productid) "
|
||||
"'Firmware='$(nvram get firmver).$(nvram get buildno) "
|
||||
"'LAN='$(nvram get lan_ipaddr) "
|
||||
"'WAN='$(nvram get wan0_ipaddr) "
|
||||
"'WiFi24='$(nvram get wl0_radio) "
|
||||
"'WiFi5='$(nvram get wl1_radio); uptime"
|
||||
)
|
||||
return f"<pre>{html.escape(output)}</pre>"
|
||||
|
||||
|
||||
def wifi():
|
||||
output = _run(
|
||||
"printf '%s\\n' "
|
||||
"'2.4GHz='$(nvram get wl0_radio) "
|
||||
"'5GHz='$(nvram get wl1_radio) "
|
||||
"'6GHz='$(nvram get wl2_radio)"
|
||||
)
|
||||
return f"<pre>{html.escape(output)}</pre>"
|
||||
|
||||
|
||||
def clientes():
|
||||
output = _run("cat /proc/net/arp")
|
||||
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}
|
||||
print(actions.get(action, estado)())
|
||||
Reference in New Issue
Block a user