Created
August 2, 2023 10:26
-
-
Save gmikhail/5a275f77affad91dcc83236144ffb49e to your computer and use it in GitHub Desktop.
Script to reboot router Huawei WS319
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Adapted for Huawei WS319 | |
# Original script: https://github.com/relay7/files.matthuisman.nz/blob/master/hg659_reboot.py | |
import sys, json, re, base64, hashlib | |
from requests import session | |
from bs4 import BeautifulSoup | |
############ CONFIG ################# | |
IP_ADDR = '192.168.1.1' | |
USERNAME = 'kyivstar' | |
PASSWORD = 'kyivstar' | |
##################################### | |
s = session() | |
pass_hash = hashlib.sha256(PASSWORD.encode()).hexdigest() | |
pass_hash = base64.b64encode(pass_hash.encode()).decode() | |
## INITIAL CSRF ## | |
try: | |
r = s.get('http://{0}/html/index.html'.format(IP_ADDR)) | |
html = BeautifulSoup(r.text, 'html.parser') | |
data = { | |
'csrf_param': html.find('meta', {'name':'csrf_param'}).get('content'), | |
'csrf_token': html.find('meta', {'name':'csrf_token'}).get('content'), | |
} | |
assert data['csrf_param'] and data['csrf_token'], 'Empty csrf_param or csrf_token' | |
print("Acquired CSRF") | |
except Exception as e: | |
print('Failed to get CSRF: {0}'.format(e)) | |
sys.exit(1) | |
## LOGIN ## | |
try: | |
data = {'csrf':{'csrf_param':data['csrf_param'],'csrf_token':data['csrf_token']}, 'data':{'UserName':USERNAME,'Password':pass_hash}} | |
r = s.post('http://{0}/api/system/user_login'.format(IP_ADDR), data=json.dumps(data, separators=(',', ':'))) | |
data = json.loads(re.search('({.*?})', r.text).group(1)) | |
assert data.get('errorCategory','').lower() == 'ok', data | |
print("Logged in") | |
except Exception as e: | |
print('Failed to login: {0}'.format(e)) | |
sys.exit(1) | |
## REBOOT ## | |
try: | |
data = {'csrf':{'csrf_param':data['csrf_param'],'csrf_token':data['csrf_token']}} | |
r = s.post('http://{0}/api/service/reboot.cgi'.format(IP_ADDR), data=json.dumps(data, separators=(',', ':'))) | |
data = json.loads(re.search('({.*?})', r.text).group(1)) | |
assert data['errcode'] == 0, data | |
print("Rebooting") | |
except Exception as e: | |
print('Failed to reboot: {0}'.format(e)) | |
sys.exit(1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment