#!/usr/bin/env python """ This scripts monitors the MR1100 to re-enable Data automatically. This is useful with operators that use PPP and which systematically disconnect users after a certain period of time. Works with firmware 12.06.08.00 'autoconnect': 'Always', """ import requests import json import time import datetime import sys import logging logging.basicConfig(format='%(asctime)s - %(message)s', datefmt='%d-%b-%y %H:%M:%S') ip = "192.168.1.1" pwd = "T6N9NLMb" timeout = 5 url_base = "http://{}".format(ip) url_session = "{}/sess_cd_tmp".format(url_base) url_js_token = "{}/api/model.json".format(url_base) url_config = "{}/Forms/config".format(url_base) url_json = "{}/api/model.json".format(url_base) url_json_success = "{}/success.json".format(url_base) url_success = "{}/index.html".format(url_base) def login(r): try: api = r.get(url_js_token, timeout=timeout).text except: logging.warning ("Failed to connect to router") return try: api = json.loads(api) global token token = api['session']['secToken'] except: logging.warning ("Failed to fetch authentification token") return data = { 'token': token, 'err_redirect': '/index.html?loginfailed', 'ok_redirect': '/index.html', 'session.password': pwd, } try: redirect_url = r.post(url_config, data=data, timeout=timeout).url if redirect_url == url_success: return True else: logging.warning ("Failed to authenticate, incorrect password?") return except: logging.warning ("Router failed to respond to authentication challenge") return def get_status(r): try: json_model = json.loads(r.get(url_json, timeout=10).text) status = json_model['wwan']['connection'] return status except: logging.warning("Cannot retrieve connection status") def reconnect(r): disconnect = { 'token': token, 'err_redirect': '/error.json', 'ok_redirect': '/success.json', 'wwan.autoconnect': 'Never', } connect = { 'token': token, 'err_redirect': '/error.json', 'ok_redirect': '/success.json', 'wwan.autoconnect': 'Always', } try: push = r.post(url_config, data=disconnect, timeout=timeout) if push.url != url_json_success: logging.warning("Failed to disconnect data") except: logging.warning("Invalid answer from router") return try: push = r.post(url_config, data=connect, timeout=timeout) if push.url != url_json_success: logging.warning("Failed to reconnect data") return except: logging.warning("Invalid answer from router") def internet(): url = "http://www.google.com" timeout = 10 try: request = requests.get (url, timeout=timeout) return True except: logging.warning("No internet connection") return False def main(dry_run=None): data_status = None r = requests.Session() login_status = login(r) if login_status != True: return else: data_status = get_status(r) if dry_run is True: return True if data_status != "Connected": logging.warning("Data appears to be disconnected, reconnecting..") reconnect(r) nb_tries = 0 while data_status != "Connected" or nb_tries < 3: data_status = get_status(r) time.sleep(1) nb_tries+=1 if data_status == "Connected": logging.warning ("Reconnection successful") else: logging.warning ("Data is still offline, this should not happen") else: if internet() != True: logging.warning ("Data appears to be disconnected, reconnecting..") reconnect(r) nb_tries = 0 while data_status != "Connected" or nb_tries < 3: data_status = get_status(r) time.sleep(1) nb_tries+=1 if data_status == "Connected": logging.warning ("Reconnection successful") else: logging.warning ("Data is still offline, this should not happen") else: return if __name__ == '__main__': try: dry_run = main(dry_run=True) if dry_run is None: sys.exit(1) else: logging.warning("Connection to router successful. Monitoring disconnections") while True: main() time.sleep(60) except KeyboardInterrupt: sys.exit(0)