Box 4K script pour la rebooter

Le forum dédié aux modems ou décodeurs (FTTH et FTTLA) de SFR ou Red by SFR (LaBox v1, v2, 4K, Mini, STB7, Décodeur Plus, SFR Box 8) et de tout autre matériel domotique commercialisé par SFR

Modérateurs : QSonic, Odo, Nico!, Macpeace, David M, Frédéric Fellague, Esteban, Maxximum, BARNABE

Répondre
Daniel92
Messages : 232
Enregistré le : lun. 22 juil. 2019, 12:33

Box 4K script pour la rebooter

Message par Daniel92 » sam. 12 sept. 2020, 03:22

Bonsoir,

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))
la source
https://easydomoticz.com/forum/viewtopic.php?t=7048
Box 4k- 1Gb/60

bretonneux
Messages : 355
Enregistré le : mar. 20 mai 2014, 18:00
Localisation : Angouleme ftth 1000/500

Re: Box 4K script pour la rebooter

Message par bretonneux » mar. 15 sept. 2020, 10:20

cool! :-D
enfin le gigabit en fibre!!!!!!!!!!!!!!

Répondre