ci-joint un script python permettant de rebooter automatiquement la box 4k par exemple en le mettant dans une crontab. Il devrait également fonctionner sur une machine Windows (planificateur de tâches).
Code : Tout sélectionner
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# importing the requests, regular expressions and time libraries
import requests
import re
import time
ip_addr = "192.168.1.253" # IP address of the SFR box
login = "admin"
pswd = "yourpassword"
timeout = 3
try:
#########################################################################################################
# Connect to login page and get auth token
URL = "http://" + ip_addr + "/login.html"
print("Opening " + URL)
r = requests.get(url = URL, headers={'Connection':'keep-alive'}, timeout = timeout)
r.raise_for_status()
# The login page contains the Session Key declared as follows : var SessionKey = '273582092';
# --> get it with regular expression
SessionKey = "0"
items=re.findall("SessionKey =.*$",r.text,re.MULTILINE)
for x in items:
print(x)
m = re.match(r'SessionKey = \'(.*)\';', x)
SessionKey = m.group(1)
print(SessionKey)
if(SessionKey == "0"):
raise ValueError('SessionKey not found in ' + URL)
time.sleep(1)
#########################################################################################################
# Submit login, password and session key to the login page with a post like the user would do it
URL = "http://" + ip_addr + "/goform/login?sessionKey=" + SessionKey
# Data to be sent to api
data = {'loginUsername':login,
'loginPassword':pswd}
# Send post request and save response as response object
print("Opening " + URL)
r = requests.post(url = URL, data = data, headers={'Connection':'keep-alive'}, timeout = timeout)
r.raise_for_status()
# Extract the new Session Key provided by the admin page
SessionKey = "0"
items=re.findall("SessionKey =.*$",r.text,re.MULTILINE)
for x in items:
print(x)
m = re.match(r'SessionKey = \'(.*)\';', x)
SessionKey = m.group(1)
print(SessionKey)
if(SessionKey == "0"):
raise ValueError('SessionKey not found in ' + URL)
time.sleep(1)
#########################################################################################################
# Call reboot service
URL = "http://" + ip_addr + "/goform/WebUiOnlyReboot?sessionKey=" + SessionKey
data = {'sessionKey':SessionKey}
# sending post request and saving response as response object
print("Opening " + URL)
r = requests.post(url = URL, headers={'Connection':'keep-alive','Content-Type':'application/x-www-form-urlencoded','Referer':'http://' + ip_addr + '/config.html'}, timeout = timeout)
r.raise_for_status()
# DONE, waiting for the modem to reboot!
except requests.exceptions.Timeout as e:
print("TimeoutError! :" + str(e))
except requests.exceptions.TooManyRedirects as e:
print("Bad URL with too many redirection! :" + str(e))
except requests.exceptions.RequestException as e:
print("Request exception! :" + str(e))
except requests.exceptions.HTTPError as e:
print("HTTP error! :" + str(e))
except ValueError as e:
print(str(e))
https://easydomoticz.com/forum/viewtopic.php?t=7048