86 lines
No EOL
2.7 KiB
Python
86 lines
No EOL
2.7 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
Fritz!Box Neustart via TR-064 (UPnP/SOAP auf Port 49000).
|
|
|
|
Konfiguration: /etc/fritz-reboot.conf
|
|
FRITZ_IP=192.168.178.1
|
|
FRITZ_USER=ein_dafuer_angelegter_nutzer
|
|
FRITZ_PASS=das_geheime_passwort
|
|
|
|
Voraussetzung in der Fritz!Box:
|
|
Heimnetz -> Netzwerkeinstellungen -> Erweiterte Netzwerkeinstellungen
|
|
[x] Zugriff fuer Anwendungen zulassen
|
|
[x] Statusinformationen ueber UPnP uebertragen
|
|
Der angegebene Benutzer muss das Recht "Fritz!Box-Einstellungen" haben.
|
|
"""
|
|
import sys
|
|
import datetime
|
|
import urllib.request
|
|
import urllib.error
|
|
|
|
CONFIG_FILE = "/etc/fritz-reboot.conf"
|
|
LOG_FILE = "/var/log/fritz-reboot.log"
|
|
|
|
|
|
def read_config():
|
|
cfg = {}
|
|
with open(CONFIG_FILE) as f:
|
|
for line in f:
|
|
line = line.strip()
|
|
if "=" in line and not line.startswith("#"):
|
|
k, v = line.split("=", 1)
|
|
cfg[k.strip()] = v.strip()
|
|
return cfg
|
|
|
|
|
|
def log(msg):
|
|
line = datetime.datetime.now().strftime("%c") + ": " + msg
|
|
print(line)
|
|
with open(LOG_FILE, "a") as f:
|
|
f.write(line + "\n")
|
|
|
|
|
|
def tr064_reboot(ip, user, pw):
|
|
url = f"http://{ip}:49000/upnp/control/deviceconfig"
|
|
soap = (
|
|
'<?xml version="1.0" encoding="utf-8"?>\n'
|
|
'<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/" '
|
|
's:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">\n'
|
|
' <s:Body>\n'
|
|
' <u:Reboot xmlns:u="urn:dslforum-org:service:DeviceConfig:1"/>\n'
|
|
' </s:Body>\n'
|
|
'</s:Envelope>\n'
|
|
)
|
|
|
|
pwmgr = urllib.request.HTTPPasswordMgrWithDefaultRealm()
|
|
pwmgr.add_password(None, url, user, pw)
|
|
auth = urllib.request.HTTPDigestAuthHandler(pwmgr)
|
|
opener = urllib.request.build_opener(auth)
|
|
|
|
req = urllib.request.Request(
|
|
url,
|
|
data=soap.encode("utf-8"),
|
|
headers={
|
|
"Content-Type": 'text/xml; charset="utf-8"',
|
|
"SOAPAction": '"urn:dslforum-org:service:DeviceConfig:1#Reboot"',
|
|
},
|
|
)
|
|
return opener.open(req, timeout=10)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
cfg = read_config()
|
|
try:
|
|
resp = tr064_reboot(cfg["FRITZ_IP"], cfg["FRITZ_USER"], cfg["FRITZ_PASS"])
|
|
log(f"TR-064 Reboot ausgeloest (HTTP {resp.status}).")
|
|
except urllib.error.HTTPError as e:
|
|
body = e.read().decode("utf-8", errors="replace")[:300]
|
|
log(f"FEHLER HTTP {e.code} {e.reason}: {body}")
|
|
sys.exit(1)
|
|
except urllib.error.URLError as e:
|
|
log(f"FEHLER Verbindung: {e} -- TR-064 aktiviert? "
|
|
"Heimnetz > Mesh > Netzwerkeinstellungen > 'Zugriff fuer Anwendungen zulassen'")
|
|
sys.exit(1)
|
|
except Exception as e:
|
|
log(f"FEHLER: {e}")
|
|
sys.exit(1) |