Diese Dateien liefen nur auf dem NAS und fehlten im Repository. Ab jetzt ist Gitea die Quelle, der NAS holt per git pull. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
103 lines
4.3 KiB
Python
103 lines
4.3 KiB
Python
"""OpenWrt Router SSH Client.
|
|
|
|
Zugang aus der Umgebung: OPENWRT_HOST, OPENWRT_USER, optional OPENWRT_KEY_FILE oder OPENWRT_PASSWORD.
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
import os
|
|
import paramiko
|
|
|
|
def _host() -> str:
|
|
return os.environ.get("OPENWRT_HOST", "192.168.1.1")
|
|
|
|
def _user() -> str:
|
|
return os.environ.get("OPENWRT_USER", "root")
|
|
|
|
def _client() -> paramiko.SSHClient:
|
|
ssh = paramiko.SSHClient()
|
|
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
|
|
key_file = os.environ.get("OPENWRT_KEY_FILE")
|
|
if key_file and os.path.isfile(key_file):
|
|
try:
|
|
pkey = paramiko.Ed25519Key.from_private_key_file(key_file)
|
|
except Exception:
|
|
pkey = paramiko.RSAKey.from_private_key_file(key_file)
|
|
ssh.connect(_host(), username=_user(), pkey=pkey, timeout=10, banner_timeout=10)
|
|
else:
|
|
pw = os.environ.get("OPENWRT_PASSWORD")
|
|
if not pw:
|
|
raise SystemExit("Weder OPENWRT_KEY_FILE noch OPENWRT_PASSWORD gesetzt")
|
|
ssh.connect(_host(), username=_user(), password=pw, timeout=10, banner_timeout=10)
|
|
return ssh
|
|
|
|
def exec_cmd(command: str) -> dict:
|
|
"""Führt einen Befehl auf dem OpenWrt-Router aus und gibt stdout/stderr zurück."""
|
|
with _client() as ssh:
|
|
_, stdout, stderr = ssh.exec_command(command, timeout=30)
|
|
out = stdout.read().decode("utf-8", "replace").strip()
|
|
err = stderr.read().decode("utf-8", "replace").strip()
|
|
return {"command": command, "stdout": out, "stderr": err}
|
|
|
|
def system_info() -> dict:
|
|
"""Router-Status: Uptime, CPU-Last, RAM-Verbrauch, Firmware-Version."""
|
|
with _client() as ssh:
|
|
_, out_up, _ = ssh.exec_command("uptime", timeout=5)
|
|
_, out_free, _ = ssh.exec_command("free -m", timeout=5)
|
|
_, out_rel, _ = ssh.exec_command("cat /etc/openwrt_release", timeout=5)
|
|
return {
|
|
"uptime": out_up.read().decode("utf-8", "replace").strip(),
|
|
"memory": out_free.read().decode("utf-8", "replace").strip(),
|
|
"release": out_rel.read().decode("utf-8", "replace").strip(),
|
|
}
|
|
|
|
def network_status() -> dict:
|
|
"""Netzwerk-Interfaces und IP-Adressen des Routers."""
|
|
with _client() as ssh:
|
|
_, out_ip, _ = ssh.exec_command("ip -br addr", timeout=5)
|
|
_, out_gw, _ = ssh.exec_command("ip route show default", timeout=5)
|
|
return {
|
|
"interfaces": out_ip.read().decode("utf-8", "replace").strip(),
|
|
"default_route": out_gw.read().decode("utf-8", "replace").strip(),
|
|
}
|
|
|
|
def dhcp_leases() -> list[dict]:
|
|
"""Aktive DHCP-Leases (verbundene Geräte im Heimnetz mit Hostname, IP und MAC)."""
|
|
with _client() as ssh:
|
|
_, stdout, _ = ssh.exec_command("cat /tmp/dhcp.leases", timeout=5)
|
|
lines = stdout.read().decode("utf-8", "replace").strip().splitlines()
|
|
leases = []
|
|
for line in lines:
|
|
parts = line.split()
|
|
if len(parts) >= 4:
|
|
leases.append({
|
|
"expires_epoch": parts[0],
|
|
"mac": parts[1],
|
|
"ip": parts[2],
|
|
"hostname": parts[3] if parts[3] != "*" else "(unbekannt)",
|
|
})
|
|
return leases
|
|
|
|
def stream_buffer_status() -> dict:
|
|
"""Status des stream-buffer / tvbuf IPTV-Proxys auf dem Router."""
|
|
with _client() as ssh:
|
|
_, stdout, _ = ssh.exec_command("ps | grep -E 'stream-buffer|tvbuf' | grep -v grep", timeout=5)
|
|
_, netstat, _ = ssh.exec_command("netstat -tlpn 2>/dev/null | grep -E '8087|8088|8089'", timeout=5)
|
|
return {
|
|
"processes": stdout.read().decode("utf-8", "replace").strip(),
|
|
"ports": netstat.read().decode("utf-8", "replace").strip(),
|
|
}
|
|
|
|
def service_action(service: str, action: str) -> dict:
|
|
"""Service steuern: /etc/init.d/<service> <start|stop|restart|status>."""
|
|
erlaubt = ["start", "stop", "restart", "status", "reload"]
|
|
if action not in erlaubt:
|
|
raise ValueError(f"Ungültige Aktion '{action}'. Erlaubt: {erlaubt}")
|
|
cmd = f"/etc/init.d/{service} {action}"
|
|
return exec_cmd(cmd)
|
|
|
|
def reboot() -> dict:
|
|
"""Startet den Router neu (Klick-Freigabe erforderlich)."""
|
|
with _client() as ssh:
|
|
ssh.exec_command("reboot", timeout=2)
|
|
return {"status": "rebooting", "message": "Router wird neu gestartet."}
|