def run(self): logging.debug("%sCheck if Router is online ...", LoggerSetup.get_log_deep(2)) self.router.mode = Mode.normal process = Popen(["ping", "-c", "1", self.router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode('utf-8'): logging.debug("%s[+] Router online with IP " + str(self.router.ip), LoggerSetup.get_log_deep(3)) # Try to get a IP via dhclient if Dhclient.update_ip(self.router.vlan_iface_name) == 1: logging.error("%s[-] Dhclient failed", LoggerSetup.get_log_deep(3)) return self.router.mode = Mode.configuration process = Popen(["ping", "-c", "1", self.router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode('utf-8'): logging.debug("%s[+] Router online with IP " + str(self.router.ip), LoggerSetup.get_log_deep(3)) # Try to get a IP via dhclient if Dhclient.update_ip(self.router.vlan_iface_name) == 1: logging.error("%s[-] Dhclient failed", LoggerSetup.get_log_deep(2)) return logging.debug("%s[-] Router is not online", LoggerSetup.get_log_deep(3)) self.router.mode = Mode.unknown return
def test_normal_functionality(self): print("Test Dhclient: update_ip") router = self._create_router() ipdb = IPDB() try: print("Get link-interface ...") # Get the real link interface link_iface = ipdb.interfaces["eth0"] # Create a Vlan print("Create VLAN ...") with ipdb.create(kind="vlan", ifname=router.vlan_iface_name, link=link_iface, vlan_id=router.vlan_iface_id).commit() as iface: print("Update IP with Dhclient ...") Dhclient.update_ip(router.vlan_iface_name) iface.mtu = 1400 # Because the IPDB need some time to update the given IP sleep(10) process = Popen(["ping", "-c", "1", "-I", router.vlan_iface_name, router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if not(sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode('utf-8')): raise Exception(str(sterr.decode('utf-8'))) print("[+] Test was successful") except Exception as e: print("Error: " + str(e)) raise e finally: print("Delete VLAN ...") ipdb.interfaces[router.vlan_iface_name].remove().commit() ipdb.release()
def run(self): """ Copies the firmware image onto the Router, proves if the firmware is in the right file(/tmp/<firmware_name>.bin) and does a Sysupgrade. """ network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.warning("%s[-] Couldn't sysupgrade the Router(" + str(self.router.id) + ")", LoggerSetup.get_log_deep(2)) logging.warning(str(e)) return network_ctrl.remote_system_wget(self.router.firmware.file, '/tmp/', self.web_server_ip) # sysupgrade -n <firmware_name> // -n verwirft die letzte firmware arg = '-n' if self.n else '' if not self.debug: logging.debug("%sSysupgrade (this will force a TimeoutError)...", LoggerSetup.get_log_deep(2)) try: network_ctrl.send_command('sysupgrade ' + arg + ' ' + '/tmp/' + self.router.firmware.name) except TimeoutError: try: Dhclient.update_ip(self.router.vlan_iface_name) self._success_handling() except FileExistsError: self._success_handling() pass except Exception: self._execption_hanling() except Exception: self._execption_hanling() network_ctrl.exit()
def test_timeout(self): print("Test Dhclient: timeout") router = self._create_router() # Set VLAN that doesn't exist, so no response occur. router._vlan_iface_id = 99 router._vlan_iface_name = "vlan99" ipdb = IPDB() try: print("Get link-interface ...") # Get the real link interface link_iface = ipdb.interfaces["eth0"] # Create a Vlan print("Create VLAN ...") with ipdb.create(kind="vlan", ifname=router.vlan_iface_name, link=link_iface, vlan_id=router.vlan_iface_id).commit() as iface: print("Update IP with Dhclient ...") Dhclient.update_ip(router.vlan_iface_name, timeout=10) iface.mtu = 1400 except TimeoutError: print("[+] Test was successful") except Exception as e: print("Error: " + str(e)) raise e finally: print("Delete VLAN ...") ipdb.interfaces[router.vlan_iface_name].remove().commit() ipdb.release()
def _wca_setup_wizard(self, config): """ Starts the WebConfigurationAssist and sets the values provided by the wizard-mode (in the WebConfiguration). :param config: {node_name, mesh_vpn, limit_bandwidth, show_location, latitude, longitude, altitude, contact,...} """ try: # remote_sytem has to be a router object wca = WebConfigurationAssist(config, self.router) wca.setup_wizard() wca.exit() except Exception as e: logging.error("%s" + str(e), LoggerSetup.get_log_deep(2)) raise e # The Router should reboot logging.info("%sWait until Router rebooted (90sec) ...", LoggerSetup.get_log_deep(2)) time.sleep(90) try: Dhclient.update_ip(self.router.vlan_iface_name) self._success_handling() except FileExistsError: self._success_handling() except Exception: self._exception_handling()
def _wait_for_ip_assignment(self) -> bool: """ Waits until the dhcp-client got an ip. :return: 'True' if we got a IP via dhclient """ logging.debug("%sWait for IP assignment via dhcp for VLAN-Interface(" + self.vlan_iface_name + ") ...", LoggerSetup.get_log_deep(3)) try: Dhclient.update_ip(self.vlan_iface_name) return True except: return False
def run(self): """ Uses the Dhlcient to get an IP from a given Router and tries to connect to. """ logging.info("%sCheck if Router is online ...", LoggerSetup.get_log_deep(1)) try: Dhclient.update_ip(self.router.vlan_iface_name) self._test_connection() except FileExistsError: self._test_connection() except Exception: logging.debug("%s[*] Try again in a minute", LoggerSetup.get_log_deep(2)) return
def run(self): """ Runs new thread and trys to send a command via ssh to reboot the Router. """ logging.info("%sReboot the Router(" + str(self.router.id) + ") ...", LoggerSetup.get_log_deep(1)) network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.error("%s[-] Couldn't reboot Router(" + str(self.router.id) + ")", LoggerSetup.get_log_deep(2)) logging.error("%s" + str(e), LoggerSetup.get_log_deep(2)) network_ctrl.exit() return # Reboot Router into configuration-mode if self.configmode: if self.router.mode == Mode.configuration: logging.info("%s[+] Router is already in " + str(Mode.configuration), LoggerSetup.get_log_deep(2)) network_ctrl.exit() return try: network_ctrl.send_command("uci set 'gluon-setup-mode.@setup_mode[0].enabled=1'") network_ctrl.send_command("uci commit") network_ctrl.send_command("reboot") logging.info("%sWait until Router rebooted (60sec) ...", LoggerSetup.get_log_deep(2)) time.sleep(60) Dhclient.update_ip(self.router.vlan_iface_name) self._success_handling() except FileExistsError: self._success_handling() pass except Exception as e: self._execption_hanling() # Reboot Router into normal-mode else: if self.router.mode == Mode.normal: logging.info("%s[+] Router is already in " + str(Mode.normal), LoggerSetup.get_log_deep(2)) network_ctrl.exit() return try: network_ctrl.send_command("reboot") logging.info("%sWait until Router rebooted (90sec) ...", LoggerSetup.get_log_deep(2)) time.sleep(90) Dhclient.update_ip(self.router.vlan_iface_name) self._success_handling() except FileExistsError: self._success_handling() pass except Exception as e: self._execption_hanling() network_ctrl.exit()
def _wait_for_ip_assignment(self) -> bool: """ Waits until the dhcp-client got an ip. :return: 'True' if we got a IP via dhclient """ logging.debug( "%sWait for IP assignment via dhcp for VLAN-Interface(" + self.vlan_iface_name + ") ...", LoggerSetup.get_log_deep(3), ) try: Dhclient.update_ip(self.vlan_iface_name) return True except: return False
def run(self): """ Copies the firmware image onto the Router, proves if the firmware is in the right file(/tmp/<firmware_name>.bin) and does a Sysupgrade. :return: """ network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.warning("[-] Couldn't sysupgrade the Router(" + str(self.router.id) + ")") logging.warning(str(e)) return network_ctrl.remote_system_wget(self.router.firmware.file, '/tmp/', self.web_server_ip) # sysupgrade -n <firmware_name> // -n verwirft die letzte firmware arg = '-n' if self.n else '' if not self.debug: logging.debug("sysupgrade ...") try: network_ctrl.send_command('sysupgrade ' + arg + ' ' + '/tmp/' + self.router.firmware.name) except TimeoutError: logging.info("%s[+] Router was set into normal mode", LoggerSetup.get_log_deep(2)) self.router.mode = Mode.configuration if Dhclient.update_ip(self.router.vlan_iface_name) == 1: self.router.mode = Mode.unknown logging.error("%s[-] Something went wrong. Use command 'online -r " + str(self.router.id) + "'", LoggerSetup.get_log_deep(2)) except Exception as e: self.router.mode = Mode.unknown logging.error("[-] Something went wrong. Use command 'online -r " + str(self.router.id) + "'") logging.error(str(e)) network_ctrl.exit()
def run(self): logging.debug("%sCheck if Router is online ...", LoggerSetup.get_log_deep(2)) self.router.mode = Mode.normal process = Popen(["ping", "-c", "1", self.router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode( 'utf-8'): logging.debug("%s[+] Router online with IP " + str(self.router.ip), LoggerSetup.get_log_deep(3)) # Try to get a IP via dhclient if Dhclient.update_ip(self.router.vlan_iface_name) == 1: logging.error("%s[-] Dhclient failed", LoggerSetup.get_log_deep(3)) return self.router.mode = Mode.configuration process = Popen(["ping", "-c", "1", self.router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode( 'utf-8'): logging.debug("%s[+] Router online with IP " + str(self.router.ip), LoggerSetup.get_log_deep(3)) # Try to get a IP via dhclient if Dhclient.update_ip(self.router.vlan_iface_name) == 1: logging.error("%s[-] Dhclient failed", LoggerSetup.get_log_deep(2)) return logging.debug("%s[-] Router is not online", LoggerSetup.get_log_deep(3)) self.router.mode = Mode.unknown return
def test_normal_functionality(self): print("Test Dhclient: update_ip") router = self._create_router() ipdb = IPDB() try: print("Get link-interface ...") # Get the real link interface link_iface = ipdb.interfaces["eth0"] # Create a Vlan print("Create VLAN ...") with ipdb.create(kind="vlan", ifname=router.vlan_iface_name, link=link_iface, vlan_id=router.vlan_iface_id).commit() as iface: print("Update IP with Dhclient ...") Dhclient.update_ip(router.vlan_iface_name) iface.mtu = 1400 # Because the IPDB need some time to update the given IP sleep(10) process = Popen( ["ping", "-c", "1", "-I", router.vlan_iface_name, router.ip], stdout=PIPE, stderr=PIPE) stdout, sterr = process.communicate() if not (sterr.decode('utf-8') == "" and "Unreachable" not in stdout.decode('utf-8')): raise Exception(str(sterr.decode('utf-8'))) print("[+] Test was successful") except Exception as e: print("Error: " + str(e)) raise e finally: print("Delete VLAN ...") ipdb.interfaces[router.vlan_iface_name].remove().commit() ipdb.release()
def run(self): """ Copies the firmware image onto the Router, proves if the firmware is in the right file(/tmp/<firmware_name>.bin) and does a Sysupgrade. """ network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.warning( "%s[-] Couldn't sysupgrade the Router(" + str(self.router.id) + ")", LoggerSetup.get_log_deep(2)) logging.warning(str(e)) return network_ctrl.remote_system_wget(self.router.firmware.file, '/tmp/', self.web_server_ip) # sysupgrade -n <firmware_name> // -n verwirft die letzte firmware arg = '-n' if self.n else '' if not self.debug: logging.debug("%sSysupgrade (this will force a TimeoutError)...", LoggerSetup.get_log_deep(2)) try: network_ctrl.send_command('sysupgrade ' + arg + ' ' + '/tmp/' + self.router.firmware.name) except TimeoutError: try: Dhclient.update_ip(self.router.vlan_iface_name) self._success_handling() except FileExistsError: self._success_handling() pass except Exception: self._execption_hanling() except Exception: self._execption_hanling() network_ctrl.exit()
def _wait_for_ip_assignment(self) -> bool: """ Waits until the dhcp-client got an ip :return: True if we got a IP via dhclient """ logging.debug("%sWait for IP assignment via dhcp for VLAN Interface(" + self.vlan_iface_name + ") ...", LoggerSetup.get_log_deep(3)) try: ret = Dhclient.update_ip(self.vlan_iface_name) if ret == 2: logging.warning("[!] dhclient already exists") elif ret == 1: logging.debug("[-] Couldn't get an IP via dhclient") else: return True return False except Exception as e: logging.debug("[-] Couldn't get an IP via dhclient") logging.error("%s" + str(e), LoggerSetup.get_log_deep(3)) return False
def run(self): """ Copies the firmware image onto the Router, proves if the firmware is in the right file(/tmp/<firmware_name>.bin) and does a Sysupgrade. :return: """ network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.warning("[-] Couldn't sysupgrade the Router(" + str(self.router.id) + ")") logging.warning(str(e)) return network_ctrl.remote_system_wget(self.router.firmware.file, '/tmp/', self.web_server_ip) # sysupgrade -n <firmware_name> // -n verwirft die letzte firmware arg = '-n' if self.n else '' if not self.debug: logging.debug("sysupgrade ...") try: network_ctrl.send_command('sysupgrade ' + arg + ' ' + '/tmp/' + self.router.firmware.name) except TimeoutError: logging.info("%s[+] Router was set into normal mode", LoggerSetup.get_log_deep(2)) self.router.mode = Mode.configuration if Dhclient.update_ip(self.router.vlan_iface_name) == 1: self.router.mode = Mode.unknown logging.error( "%s[-] Something went wrong. Use command 'online -r " + str(self.router.id) + "'", LoggerSetup.get_log_deep(2)) except Exception as e: self.router.mode = Mode.unknown logging.error( "[-] Something went wrong. Use command 'online -r " + str(self.router.id) + "'") logging.error(str(e)) network_ctrl.exit()
def _wait_for_ip_assignment(self) -> bool: """ Waits until the dhcp-client got an ip :return: True if we got a IP via dhclient """ logging.debug( "%sWait for IP assignment via dhcp for VLAN Interface(" + self.vlan_iface_name + ") ...", LoggerSetup.get_log_deep(3)) try: ret = Dhclient.update_ip(self.vlan_iface_name) if ret == 2: logging.warning("[!] dhclient already exists") elif ret == 1: logging.debug("[-] Couldn't get an IP via dhclient") else: return True return False except Exception as e: logging.debug("[-] Couldn't get an IP via dhclient") logging.error("%s" + str(e), LoggerSetup.get_log_deep(3)) return False
def _wca_setup_wizard(self, config): """ Starts the WebConfigurationAssist and sets the values provided by the wizard-mode (in the WebConfiguration) :param config: {node_name, mesh_vpn, limit_bandwidth, show_location, latitude, longitude, altitude, contact,...} """ try: # remote_sytem has to be a router object wca = WebConfigurationAssist(config, self.router) wca.setup_wizard() wca.exit() except Exception as e: logging.error(str(e), 2) raise e # The Router should reboot logging.info("Wait until Router rebooted (45sec) ...") time.sleep(45) if Dhclient.update_ip(self.router.vlan_iface_name) == 0: self.router.mode = Mode.normal logging.info("%s[+] Router was set into normal mode", LoggerSetup.get_log_deep(2)) else: logging.warning("[!] Couldn't get a new IP for Router(" + str(self.router.id) + ")")
def run(self): """ Runs new thread and trys to send a command via ssh to reboot the Router. """ logging.info("%sReboot the Router(" + str(self.router.id) + ") ...", LoggerSetup.get_log_deep(1)) network_ctrl = NetworkCtrl(self.router) try: network_ctrl.connect_with_remote_system() except Exception as e: logging.warning("[-] Couldn't reboot Router(" + str(self.router.id) + ")") logging.warning(str(e)) network_ctrl.exit() return if self.configmode: if self.router.mode == Mode.configuration: logging.info("%s[+] Router is already in configuration mode", LoggerSetup.get_log_deep(2)) network_ctrl.exit() return try: network_ctrl.send_command( "uci set 'gluon-setup-mode.@setup_mode[0].enabled=1'") network_ctrl.send_command("uci commit") network_ctrl.send_command("reboot") logging.info("Wait until Router rebooted (45sec) ...") time.sleep(45) if Dhclient.update_ip(self.router.vlan_iface_name) == 0: self.router.mode = Mode.configuration logging.info( "%s[+] Router was set into configuration mode", LoggerSetup.get_log_deep(2)) else: network_ctrl.exit() raise Exception except Exception as e: logging.warning( "%s[-] Couldn't set Router into configuration mode", LoggerSetup.get_log_deep(2)) logging.error("%s" + str(e), LoggerSetup.get_log_deep(2)) else: if self.router.mode == Mode.normal: logging.info("%s[+] Router is already in normal mode", LoggerSetup.get_log_deep(2)) network_ctrl.exit() return try: network_ctrl.send_command("reboot") logging.info("Wait until Router rebooted (45sec) ...") time.sleep(45) if Dhclient.update_ip(self.router.vlan_iface_name) == 0: self.router.mode = Mode.normal logging.info("%s+] Router was set into normal mode", LoggerSetup.get_log_deep(2)) else: network_ctrl.exit() raise Exception except Exception as e: logging.warning("%s[-] Couldn't set Router into normal mode", LoggerSetup.get_log_deep(2)) logging.error("%s" + str(e), LoggerSetup.get_log_deep(2)) network_ctrl.exit()