Skip to content

Instantly share code, notes, and snippets.

@dorianim
Last active February 21, 2022 22:35
Show Gist options
  • Save dorianim/e68e2ae6a0e8b6ba712c60ead43238a6 to your computer and use it in GitHub Desktop.
Save dorianim/e68e2ae6a0e8b6ba712c60ead43238a6 to your computer and use it in GitHub Desktop.
Tenda4g09 rebooter

Tenda4g09 rebooter

Simple script to reboot your Tenda4g09 router as soon as it looses connection to LTE. Relies on tenda4g09 Python library.

Can be used with docker or directly (eg. on a raspberry pi).

version: "3.3"
services:
tenda:
build: .
environment:
TENDA_HOST: 192.168.0.1
TENDA_PASSWORD: somepassword
labels:
# Force reboot every day at two am
ofelia.enabled: "true"
ofelia.job-exec.datecron.schedule: "0 0 2 * *"
ofelia.job-exec.datecron.command: "python3 /reboot.py"
ofelia:
image: mcuadros/ofelia:latest
command: daemon --docker
volumes:
- /var/run/docker.sock:/var/run/docker.sock:ro
- ./logs:/logs
labels:
ofelia.save-folder: "/logs"
depends_on:
- tenda
FROM python:3-alpine
RUN pip install tenda4g09
COPY ./run.py /run.py
COPY ./reboot.py /reboot.py
CMD ["/usr/local/bin/python3", "-u", "/run.py"]
import os
from tenda4g09 import Tenda4G09
tenda = Tenda4G09(os.environ['TENDA_HOST'])
success = False
# Try as may times as necessary
while not success:
success = tenda.login(os.environ['TENDA_PASSWORD'])
if success:
success = tenda.reboot()
print("Reboot OK" if success else "Reboot ERROR! Retrying...")
else:
print("Login error! Retrying...")
import os, time
from tenda4g09 import Tenda4G09
tenda = Tenda4G09(os.environ['TENDA_HOST'])
while True:
if tenda.login(os.environ['TENDA_PASSWORD']):
while True:
ret, status = tenda.status()
if not ret:
# logging in when someone else is using the api (eg. in the webinterface) will kicj that other someoue out
# -> Sleep a while to keep the webinterface usable
print("Cannot get status. Assuming someone else is using the api and sleeping for 10 minutes before logging in again.")
time.sleep(60*10)
break
elif status["simInfo"]["internetStatus"] == "1":
print("Internet OK :D")
else:
print("Internet ERROR!")
print("Reboot OK" if tenda.reboot() else "Reboot ERROR")
# Check every 5 seconds
time.sleep(5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment