Skip to content

Instantly share code, notes, and snippets.

@gkatev
Last active April 21, 2022 19:23
Show Gist options
  • Save gkatev/683db0265ec9d0d862bbd7ec47ca05e3 to your computer and use it in GitHub Desktop.
Save gkatev/683db0265ec9d0d862bbd7ec47ca05e3 to your computer and use it in GitHub Desktop.
Script to reboot Cosmote Speedport Entry 2i
#!/usr/bin/python3
import requests
import hashlib
import sys, re
modem_user = MY_USER
modem_password = MY_PASSWORD
modem_addr = "http://" + MY_ADDR
session = requests.session()
# 1. Visit the home page, to obtain necessary cookies.
# The login won't work if _TESTCOOKIESUPPORT=1 is not present.
# ------------------------------------------------------------
print("1. Visiting home page...")
req = session.get(modem_addr)
if req.status_code != 200:
print("Error visiting home page, HTTP %s" % req.status_code)
sys.exit(1)
# 2. Obtain login token
# ---------------------
print("\n2. Requesting login token...")
req = session.get(modem_addr
+ "/function_module/login_module/login_page/logintoken_lua.lua")
if req.status_code == 200:
m = re.findall(r"<\w+>(\d+)</\w+>", req.text)
login_token = m[0].encode().decode('unicode_escape')
print("Login token is %s" % login_token)
else:
print("Error: Token request failed, HTTP %s" % req.status_code);
sys.exit(2)
salted_password = modem_password + login_token
password_hash = hashlib.sha256(salted_password.encode()).hexdigest()
# 3. Perform Log in
# -----------------
print("\n3. Logging in...")
req = session.post(modem_addr + "/", data = {
"Username": modem_user,
"Password": password_hash,
"action": "login"
})
if req.status_code != 200:
print("Error logging in, HTTP %s" % req.status_code)
sys.exit(3)
elif "<div id=\"loginWrapper\">" in req.text:
print("Error: User authentication failed")
sys.exit(4)
print("Login successful")
# 4. Visit system management page to obtain session token
# -------------------------------------------------------
print("\n4. Obtaining system management session token...")
req = session.get(modem_addr + "/getpage.lua", params = {
"pid": "123",
"nextpage": "ManagDiag_DeviceManag_t.lp"
})
if req.status_code == 200:
m = re.findall(r"_sessionTmpToken = \"(.*?)\";", req.text)
session_token = m[0].encode().decode('unicode_escape')
print("Session token is %s" % session_token)
else:
print("Error visiting system management page, HTTP %s" % req.status_code)
sys.exit(5)
# 5. Perform system reboot
# ------------------------
print("\n5. Performing reboot...")
req = session.post(modem_addr + "/common_page/deviceManag_lua.lua", data = {
"IF_ACTION": "Restart",
"Btn_restart": "",
"_sessionTOKEN": session_token
})
if req.status_code == 200:
m = re.findall(r"<IF_ERRORSTR>(.*?)</IF_ERRORSTR>", req.text)
errorstr = m[0].encode().decode('unicode_escape')
if errorstr != "SUCC":
print("Error performing reboot: %s" % match.group(1))
sys.exit(6)
else:
print("Error performing reboot, HTTP %s" % req.status_code)
sys.exit(7)
print("Reboot successful")
[Unit]
Description=Reboot Speedport DSL modem
StartLimitIntervalSec=1m
[Service]
Type=oneshot
User=?
ExecStart=<SPEEDPORT REBOOT PY>
# The script will sometimes mysteriously fail with HTTP 404. Go figure...
Restart=on-failure
RestartSec=3
RestartLimitBurst=5
[Unit]
Description=Reboot Speedport DSL modem
[Timer]
OnCalendar=*-*-* hh:mm:ss
AccuracySec=10
Persistent=false
[Install]
WantedBy=timers.target
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment