From 0533b4f5e8e1458bbc14f96cf5e9db6bf9d6786d Mon Sep 17 00:00:00 2001 From: Nikolaos Karaolidis Date: Sat, 23 Apr 2022 12:55:44 +0300 Subject: [PATCH] Add reconnect script --- src/reconnect.py | 82 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/reconnect.py diff --git a/src/reconnect.py b/src/reconnect.py new file mode 100644 index 0000000..eb82edb --- /dev/null +++ b/src/reconnect.py @@ -0,0 +1,82 @@ +from requests import post +from json import dumps +from json import loads +from os import environ + +def login(username, password, url='http://192.168.1.1', path='/ws'): + headers = { + 'Accept': '*/*', + 'Authorization': 'X-Sah-Login', + 'Content-Type': 'application/x-sah-ws-4-call+json' + } + + data = { + 'service': 'sah.Device.Information', + 'method': 'createContext', + 'parameters': { + 'applicationName': 'webui', + 'username': username, + 'password': password + } + } + + response = post(url + path, headers=headers, data=dumps(data)) + + session_key = list(dict(response.cookies).keys())[0].split('/')[0] + session_value = list(dict(response.cookies).values())[0] + context_id = loads(response.text)['data']['contextID'] + + return session_key, session_value, context_id + +def cookie(session_key, session_value, context_id): + return f'UILang=en; lastKnownIpv6TabState=visible; {session_key}/accept-language=en-US,en; {session_key}/sessid={session_value}; sah/contextId={context_id}' + +def traceroute(username, password, url='http://192.168.1.1', path='/ws'): + session_key, session_value, context_id = login(username, password, url, path) + + headers = { + 'Accept': '*/*', + 'Content-Type': 'application/x-sah-ws-4-call+json', + 'Authorization': 'X-Sah ' + context_id, + 'Cookie': cookie(session_key, session_value, context_id) + } + + data = { + 'service': 'Traceroute', + 'method': 'start_diagnostic', + 'parameters': { + 'host': 'www.google.com', + 'ipversion': 'IPv4' + } + } + + return post(url + path, headers=headers, data=dumps(data)) + +def reboot(username, password, url='http://192.168.1.1', path='/ws'): + session_key, session_value, context_id = login(username, password, url, path) + + headers = { + 'Accept': '*/*', + 'Content-Type': 'application/x-sah-ws-4-call+json', + 'Authorization': 'X-Sah ' + context_id, + 'Cookie': cookie(session_key, session_value, context_id) + } + + data = { + 'service': 'NMC', + 'method': 'reboot', + 'parameters': { + 'reason': 'WebUI reboot' + } + } + + return post(url + path, headers=headers, data=dumps(data)) + +if __name__ == '__main__': + username = environ.get('ROUTER_USERNAME') + password = environ.get('ROUTER_PASSWORD') + url = environ.get('ROUTER_IP') or '192.168.1.1' + path = environ.get('ROUTER_PATH') or '/ws' + + reboot(username, password, url, path) + \ No newline at end of file