serverroom-components/routercheckandreboot.py

41 lines
1.5 KiB
Python
Raw Normal View History

2024-11-24 18:53:59 +00:00
#!/usr/bin/env python
import os,time
# Get current WAN IP Address from router
x = os.popen("ssh admin@192.168.10.254 ip addr show ppp0 | grep inet | awk '{print $2}'").read()
# Make output go through telegram-send in a container
output = "podman run -it --rm -v /media/core/Data2/Apps/telegram-send/telegram-send.conf:/etc/telegram-send.conf tsend:20230108 telegram-send -g "
# Define current runs as 0
y = 0
# Send current IPv4 WAN address to telegram-send
os.system(output+"'IPv4 is "+x+"'")
# If xxx (as in xxx.yyy.yyy.yyy) is 100 or not starting with a number (such as error messages), reboot router. No more than 30 runs.
while((x[:3] == "100" or (int(x[0]) not in [0,1,2,3,4,5,6,7,8,9])) and y < 30):
os.system(output+"Rebooting..")
os.system("ssh admin@192.168.10.254 reboot")
time.sleep(200) # Give the router some time to reboot, currently 200 seconds.
x = os.popen("ssh admin@192.168.10.254 ip addr show ppp0 | grep inet | awk '{print $2}'").read()
os.system(output+"'Now IPv4 is "+x+"'")
y += 1
# If IP does not start with 100, do nothing.
if y==0:
os.system(output+"'IPv4 does not start with 100, not rebooting.'")
# Reboot complete, now tell the user how many runs and finally restart the Syncthing relay.
elif y < 30:
os.system(output+"'yay :)'")
os.system(output+"'Router has been rebooted "+str(y)+" time(s).'")
os.system(output+"'Restarting Syncthing relay!'")
os.system("systemctl --user restart syncthing-relay")
# Complain if 30 runs are not enough
else:
os.system(output+"'So Bad ;('")