def fup_redirect(session: requests.Session) -> data.Status: status = data.Status() response = None try: response = session.get("http://www.msftconnecttest.com/redirect") response.raise_for_status() is_fup_redirect = data.normal_redirect_netloc != urlparse(response.url).netloc soup = BeautifulSoup(response.content, 'html.parser') url = "" if soup: soup = soup.find("meta") if soup: content = soup.get("content") if content: content = content.split(";") if len(content) > 1: content = content[1].split("=") if len(content) > 1: url = content[1] status.data = { "is_fup_redirect" : is_fup_redirect, "redirect_to": url } status.success = True status.message = "Success" except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") if response: status.code = response.status_code status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def decline_fup(session: requests.Session) -> data.Status: url = data.server_base_address + data.update_subscriber_profile headers = data.common_headers status = data.Status() payload = { "operation": "dailyDecline", "location": "NOID", "actionName": "dailyFupPlan" } response = None try: response = session.post(url, headers=headers, data=payload, verify=False) response.raise_for_status() response_json = response.json() if response_json["resultCode"] == "200": status.data = response_json status.success = True status.message = "Success" else: status.message = "Plan upgrade declined failed with message: {}".format(response_json["msg"]) logger.warn(status.message) status.code = int(response_json["resultCode"]) except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") status.message = str(request_exception) if response: status.code = response.status_code except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def details(self): status = data.Status() response = None try: response = self._session.get(self.router_address + "status/status_deviceinfo.htm", auth=(self._username, self._password)) response.raise_for_status() dt = Details() soup = BeautifulSoup(response.content, features="html.parser") cells = soup.findAll("td") dt.model_number = cells[10].get_text().strip() dt.software_version = cells[15].get_text().strip() dt.hardware_version = cells[20].get_text().strip() dt.serial_number = cells[25].get_text().strip() dt.mac_address = cells[30].get_text().strip() dt.lan_ip_address = cells[40].get_text().strip() dt.lan_subnet_mask = cells[45].get_text().strip() dt.lan_dhcp_server = cells[50].get_text().strip() dt.wan_status = cells[65].get_text().strip() dt.wan_connection_type = cells[70].get_text().strip() dt.wan_ip_address = cells[76].get_text().strip() dt.wan_subnet_mask = cells[82].get_text().strip() dt.wan_default_gateway = cells[87].get_text().strip() dt.wan_primary_dns = cells[92].get_text().strip() dt.wan_secondary_dns = cells[97].get_text().strip() dt.wan_nat = cells[102].get_text().strip() dt.wan_ppp_connection_time = cells[107].get_text().strip() dt.ipv6_status = cells[117].get_text().strip() dt.ipv6_ip_address = cells[122].get_text().strip() dt.ipv6_prefix_length = cells[127].get_text().strip() dt.ipv6_default_gateway = cells[132].get_text().strip() dt.ipv6_dns_server = cells[137].get_text().strip() dt.ipv6_prefix_delegation = cells[142].get_text().strip() dt.adsl_firmware_version = cells[152].get_text().strip() dt.adsl_line_state = cells[157].get_text().strip() dt.adsl_modulation = cells[162].get_text().strip() dt.adsl_annex_mod = cells[167].get_text().strip() dt.adsl_snr_margin_downstream = cells[184].get_text().strip() + " db" dt.adsl_snr_margin_upstream = cells[185].get_text().strip() + " db" dt.adsl_line_attenuation_downstream = cells[191].get_text().strip() + " db" dt.adsl_line_attenuation_upstream = cells[192].get_text().strip() + " db" dt.adsl_data_rate_downstream = cells[198].get_text().strip() + " kbps" dt.adsl_data_rate_upstream = cells[199].get_text().strip() + " kbps" status.data = dt status.success = True status.message = "Success" except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") status.code = response.status_code status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def activate_plan(session: requests.Session, plan_name: str) -> data.Status: activate_choices = data.ActivateChoices() headers = data.common_headers status = data.Status() if plan_name == activate_choices.promotional_daily_4gb: url = data.server_base_address + data.promotional_daily_4gb_url payload = { 'planId': '-1', 'location': 'NOID', 'actionName': 'dailyFupPlan' } elif plan_name == activate_choices.promotional_monthly_10gb: url = data.server_base_address + data.promotional_monthly_10gb_url payload = { 'location': 'NOID', 'actionName': 'manual' } else: status.message = "Unknown plan selected." logger.warn(status.message) return status response = None try: response = session.post(url, headers=headers, data=payload, verify=False) response.raise_for_status() response_json = response.json() status.data = response_json if response_json["resultCode"] == "200": status.message = "Plan '{}' activated with message: {}".format(response_json["planName"], response_json["msg"]) status.success = True else: status.code = int(response_json["resultCode"]) status.message = "Plan activation failed with message: {}".format(response_json["msg"]) logger.warn(status.message) except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") status.message = str(request_exception) if response: status.code = response.status_code except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def restart(self) -> data.Status: status = data.Status() if self._username and self._password: payload = "restoreFlag=0" url = self.router_address + "Forms/tools_system_1" response = None try: response = requests.request("POST", url, data=payload, auth=(self._username, self._password)) response.raise_for_status() status.message = "Router Restarted." status.success = True except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") status.code = response.status_code status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) else: status.message = "Login details not provided" return status
def authenticate(self, username, password) -> data.Status: status = data.Status() response = None try: response = self._session.get(self.router_address, auth=(username, password)) response.raise_for_status() self._username = username self._password = password status.message = "Authentication Successful." status.success = True except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") status.code = response.status_code if response.status_code == 401: status.message = "Invalid username or password." else: status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def fetch(session: requests.Session) -> data.Status: status = data.Status() response = None try: response = session.post( 'https://fuptopup.bsnl.co.in/fetchUserQuotaPM.do', headers=headers, data=payload, verify=False) response.raise_for_status() status.data = response.json() status.success = True status.message = "Success" except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") if response: status.code = response.status_code status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def fetch_fup_param(session: requests.Session) -> data.Status: url = data.server_base_address + data.location_by_ip status = data.Status() payload = { "actionName": "dailyFupPlan", "location": ""} response = None headers = data.common_headers try: response = session.post(url, data=payload, headers= headers) response.raise_for_status() status.data = response.json() status.success = True status.message = "Success" except requests.exceptions.RequestException as request_exception: logger.exception("Error in making request.") if response: status.code = response.status_code status.message = str(request_exception) except Exception as error: logger.exception("General error occurred.") status.message = str(error) return status
def switch(args): display_choices = data.DisplayChoices() router_choices = data.RouterChoices() activate_choices = data.ActivateChoices() statuses = {} session = requests.Session() if args.display: for choice in args.display: if choice == display_choices.fup_details: usage_data = usage.fetch(session) statuses[choice] = usage_data if usage_data.success: fup_redirect_data = helpers.fup_redirect(session) statuses[choice] = fup_redirect_data if fup_redirect_data.success: fup_param = helpers.fetch_fup_param(session) statuses[choice] = fup_param if fup_param.success: combined_fup_data = helpers.map_fup_details( fup_redirect_data.data, fup_param.data, usage_data.data) if not args.json: display.fup_details(combined_fup_data) status = data.Status() status.success = True status.message = "Success" status.data = combined_fup_data statuses[choice] = status if choice == display_choices.router_details: if args.router: cls = getattr(router, args.router) router_instance = cls() status = router_instance.authenticate( args.username, args.password) if status.success: status = router_instance.details() if status.success: if not args.json: display.router_details(status.data) statuses[choice] = status else: status = data.Status() status.message = "Please add router model." statuses[choice] = status if choice == display_choices.data_usage: usage_data = usage.fetch(session) statuses[choice] = usage_data if usage_data.success: if not args.json: display.data_usage(usage_data.data) if args.decline: status = helpers.decline_fup(session) statuses["decline"] = status if status.success and not args.json: display.a_dict(status.data, "decline") if args.activate: for choice in args.activate: status = helpers.activate_plan(session, choice) statuses[choice] = status if status.success and not args.json: display.a_dict(status.data, choice) if args.restart: if args.router: cls = getattr(router, args.router) router_instance = cls() status = router_instance.authenticate(args.username, args.password) if status.success: status = router_instance.restart() if status.success and not args.json: display.a_status(status, "restart") statuses["restart"] = status else: status = data.Status() status.message = "Please add router model." statuses[choice] = status if args.json: display.as_json(statuses) else: display.errors(statuses)