def test_port_validation(self): port_test = DrayTekWebAdmin(hostname="myhost", username="******", password="******", port="8080") self.assertEqual("https://myhost:8080", port_test.url) with self.assertRaises(ValueError): port_test.port = 100000 with self.assertRaises(ValueError): port_test.port = "Not a Port Number"
def test_empty(self): default = DrayTekWebAdmin() self.assertIsNone(default.hostname) self.assertEqual("admin", default.username) self.assertIsNone(default.password) self.assertEqual(443, default.port) self.assertTrue(default.use_https) self.assertFalse(default.reboot_required)
def test_valid(self): valid = DrayTekWebAdmin( hostname="192.168.0.1", username="******", password="******", port=80, use_https=False, ) self.assertEqual("http://192.168.0.1:80", valid.url) self.assertFalse(valid.use_https)
def main(): """Main. Called when program called directly from the command line. """ argv = None parser = _get_parser() args = parser.parse_args(argv) host, port, https = parse_address_url_to_host(args.address) test_settings = TestSettings( config_dir=args.config, browser=args.browser, headless=args.headless, search_driver=args.search_driver, implicit_wait_time=args.implicit_wait, explicit_wait_time=args.explicit_wait, ) webadmin_session = None try: webadmin_session = DrayTekWebAdmin( hostname=host, port=port, use_https=https, username=args.user, password=args.password, config_dir=test_settings.config_dir, browser=test_settings.browser, headless=test_settings.headless, search_driver=test_settings.search_driver, implicit_wait_time=test_settings.implicit_wait_time, explicit_wait_time=test_settings.explicit_wait_time, ) webadmin_session.start_session() dataset = read_data(webadmin_session) save_to_csv(dataset, args.output) except Exception as exception: LOGGER.critical(exception) if webadmin_session is not None: if args.debug: timestamp = time.strftime("%Y%m%d-%H%M%S") # Collect the information from the session object and close it before attempting file access in case that fails # This avoids having another try/except/finally block within this error handling routine. hostname = webadmin_session.hostname page_source = webadmin_session.session.driver.page_source webadmin_session.close_session() debugfile = open( f"draytek_read_settings_debug-{hostname}-{timestamp}.html", "w+") debugfile.write(page_source) debugfile.close() finally: if webadmin_session is not None: webadmin_session.close_session()
def test_default(self): default = DrayTekWebAdmin(hostname="myhost", username="******", password="******") self.assertEqual("myhost", default.hostname) self.assertEqual("user123", default.username) self.assertEqual("secret", default.password) self.assertEqual(443, default.port) self.assertTrue(default.use_https) self.assertEqual("https://myhost:443", default.url) self.assertFalse(default.reboot_required)
def test_hostname_validation(self): good_names = [ "192.168.0.1", "fdfe:5d4d:384d:1:d8df:ed2:eafa:70af", "router.domain.com", "router", ] bad_names = [ "192.168.100.500", "fdfe:5d4d:384d:1:d8df:ed2:eafa:70af:1abc", "--not-a-domain.com", ] host_test = DrayTekWebAdmin(hostname="myhost", username="******", password="******") for hostname in good_names: host_test.hostname = hostname self.assertEqual(hostname, host_test.hostname) for hostname in bad_names: with self.assertRaises(ValueError) as cm: host_test.hostname = hostname self.assertEqual(f"Invalid hostname: {hostname}", str(cm.exception).rstrip())
def extract_settings(router_settings, config_dir=None, delimiter="|"): """Extracts connection settings and firmware details from csv. Logs errors if unexpected modulenames found in header. :param router: row from CSV with settings for a single router :param config_dir: path to configuration file for toolium :param delimiter: character used to separate modules from fields (default '|') :returns: dictionary of dictionaries containing extracted data from csv """ if not config_dir: LOGGER.debug( "Config directory not specified. Attempt to use cwd \\ conf") config_dir = Path(getcwd(), "conf") if Path.exists(Path(config_dir)): LOGGER.info(f"Setting configuration Directory to: {config_dir}") config_dir = str(Path(config_dir)) else: config_dir = None connection = DrayTekWebAdmin(config_dir=config_dir) router_firmware = Firmware() invalid = False try: for key in router_settings.keys(): modulename, fieldname = key.split(sep=delimiter) modulename = modulename.lower() if modulename == type(connection).__name__.lower(): if hasattr(connection, fieldname): setattr(connection, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(router_firmware).__name__.lower(): if hasattr(router_firmware, fieldname): setattr(router_firmware, fieldname, router_settings.get(key)) else: invalid = True if invalid: LOGGER.error( f"Ignoring unexpected field: {modulename}|{fieldname}") invalid = False except AttributeError as e: LOGGER.error(f"Error parsing CSV contents. {e}") settings = dict() settings["connection"] = connection settings["firmware"] = router_firmware return settings
def extract_settings(router_settings, separator="|"): """Extracts settings from csv file into dictionaries. Logs errors if unexpected modulenames found in header. :param router: row from CSV with settings for a single router :param separator: character used to separate modules from fields (default '|') :returns: settings - dictionary of dictionaries containing extracted data from csv """ connection = DrayTekWebAdmin() info = {} snmpipv4 = SNMPIPv4() snmpipv6 = SNMPIPv6() snmp_trap_ipv4 = SNMPTrapIPv4() snmp_trap_ipv6 = SNMPTrapIPv6() snmpv3 = SNMPv3() iac = InternetAccessControl() access_list = AccessList() management_port = ManagementPort() brute_force = BruteForceProtection() encryption = Encryption() cvm_access = CVM_AccessControl() device_management = DeviceManagement() ap_management = AP_Management() lan_access = LAN_Access() ipv6_management = IPv6Management() invalid = False try: for key in router_settings.keys(): modulename, fieldname = key.split(sep=separator) modulename = modulename.lower() if modulename == type(connection).__name__.lower(): if hasattr(connection, fieldname): setattr(connection, fieldname, router_settings.get(key)) else: invalid = True elif modulename == "routerinfo": info[fieldname] = router_settings.get(key) elif modulename == type(snmpipv4).__name__.lower(): if hasattr(snmpipv4, fieldname): setattr(snmpipv4, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(snmpipv6).__name__.lower(): if hasattr(snmpipv6, fieldname): setattr(snmpipv6, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(snmp_trap_ipv4).__name__.lower(): if hasattr(snmp_trap_ipv4, fieldname): setattr(snmp_trap_ipv4, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(snmp_trap_ipv6).__name__.lower(): if hasattr(snmp_trap_ipv6, fieldname): setattr(snmp_trap_ipv6, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(snmpv3).__name__.lower(): if hasattr(snmpv3, fieldname): setattr(snmpv3, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(iac).__name__.lower(): if hasattr(iac, fieldname): setattr(iac, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(access_list).__name__.lower(): if hasattr(access_list, fieldname): setattr(access_list, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(management_port).__name__.lower(): if hasattr(management_port, fieldname): setattr(management_port, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(brute_force).__name__.lower(): if hasattr(brute_force, fieldname): setattr(brute_force, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(encryption).__name__.lower(): if hasattr(encryption, fieldname): setattr(encryption, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(cvm_access).__name__.lower(): if hasattr(cvm_access, fieldname): setattr(cvm_access, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(device_management).__name__.lower(): if hasattr(device_management, fieldname): setattr(device_management, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(ap_management).__name__.lower(): if hasattr(ap_management, fieldname): setattr(ap_management, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(lan_access).__name__.lower(): if hasattr(lan_access, fieldname): setattr(lan_access, fieldname, router_settings.get(key)) else: invalid = True elif modulename == type(ipv6_management).__name__.lower(): if hasattr(ipv6_management, fieldname): setattr(ipv6_management, fieldname, router_settings.get(key)) else: invalid = True if invalid: LOGGER.error(f"Ignoring unexpected field: {modulename}|{fieldname}") invalid = False except AttributeError as e: LOGGER.error(f"Error parsing CSV contents. {e}") settings = dict() settings["connection"] = connection settings["info"] = info settings["snmpipv4"] = snmpipv4 settings["snmpipv6"] = snmpipv6 settings["snmp_trap_ipv4"] = snmp_trap_ipv4 settings["snmp_trap_ipv6"] = snmp_trap_ipv6 settings["snmpv3"] = snmpv3 settings["iac"] = iac settings["access_list"] = access_list settings["management_port"] = management_port settings["brute_force"] = brute_force settings["encryption"] = encryption settings["cvm_access"] = cvm_access settings["device_management"] = device_management settings["ap_management"] = ap_management settings["lan_access"] = lan_access settings["ipv6_managament"] = ipv6_management return settings