def _parse_dhcp_settings(html): regex = '//\s+nvram = \{(.+?)\};\n\nxob = ' match_object = re.compile(regex, re.DOTALL).search(html) if match_object is None: raise RouterParseError('Cannot parse DHCP settings') array = _parse_data_structure(match_object.group(1)) try: ip_start, ip_end = array['dhcpd_startip'], array['dhcpd_endip'] is_enabled = array['lan_proto'] == 'dhcp' except KeyError: raise RouterParseError('Bad nvram for DHCP settings') if not validator.is_valid_ip_address(ip_start): raise RouterParseError('Invalid DHCP start IP: %s' % ip_start) if not validator.is_valid_ip_address(ip_end): raise RouterParseError('Invalid DHCP end IP: %s' % ip_end) obj = DHCPServerSettings() obj.set_enabled_status(is_enabled) obj.set_ip_start(ip_start) obj.set_ip_end(ip_end) obj.ensure_valid() return obj
def parse_dhcp_settings(html): is_enabled = '<INPUT TYPE=CHECKBOX NAME=_HE CHECKED>' in html # 192.168.1 is hardcoded.. only the last part changes regex = re.compile('<INPUT NAME=HR0 VALUE="([0-9]+)" SIZE=3 MAXLENGTH=3>') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse dhcp start ip!') ip_last_part = match_object.group(1) ip_start = '192.168.1.%s' % ip_last_part # get the pool size to determine what the last IP is regex = re.compile('document.forms\[0\]\._HR1\.value=([0-9]+)-([0-9]+)') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse dhcp pool size!') num1, num2 = map(int, match_object.groups()) pool_size = num1 - num2 ip_start_long = converter.ip2long(ip_start) ip_end_long = ip_start_long + pool_size ip_end = converter.long2ip(ip_end_long) settings = DHCPServerSettings() settings.set_enabled_status(is_enabled) settings.set_ip_start(ip_start) settings.set_ip_end(ip_end) settings.ensure_valid() return settings
def _parse_dhcp_settings(html): regex = re.compile('name="dhcpRange(?:Start|End)" size="15" maxlength="15" value="(.+?)"') try: ip_start, ip_end = regex.findall(html) except ValueError: raise RouterParseError('Cannot find DHCP start/end range') else: settings = DHCPServerSettings() settings.set_ip_start(ip_start) settings.set_ip_end(ip_end) settings.set_enabled_status('<option selected value="2"><script>dw(Enable)</script></option>' in html) settings.ensure_valid() return settings