def _parse_connected_clients_list(contents): regex = "<form(?:.+?)<tr(?:.+?)<tr bgcolor=(?:.+?)<font size=2>(.+?)</td><td><font size=2>(.+?)</td><td><font size=2>([0-9]+)</td>" regex_dhcp_list = re.compile(regex, re.DOTALL) lst = ConnectedClientsList() for id, match_groups in enumerate(re.findall(regex_dhcp_list, contents), start=1): ip, mac, lease_time = match_groups if not validator.is_valid_ip_address(ip): raise RouterParseError("Invalid IP address: %s" % ip) if not validator.is_valid_mac_address(mac): raise RouterParseError("Invalid MAC address: %s" % mac) lease_time = int(lease_time) item = ConnectedClientsListItem() item.set_client_name("Client %d" % id) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(lease_time) lst.append(item) return lst
def parse_connected_clients_list(html): lst = ConnectedClientsList() regex = re.compile("var x=new Array\((.+?)\);") match_object = regex.search(html) if match_object is None: raise RouterParseError("Cannot parse connected clients list") match_str = match_object.group(1) if match_str == '""': return lst import ast try: list_literal = "[%s]" % match_str list_items = ast.literal_eval(list_literal) list_items.pop() # empty 'sentinel' string - not needed list_items = [v.decode("utf-8", "ignore") if isinstance(v, bytes) else v for v in list_items] for ip, name, mac in split_in_groups(list_items, 3): item = ConnectedClientsListItem() item.set_client_name(name) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(item.__class__.LEASE_TIME_PERMANENT) lst.append(item) return lst except Exception: return lst
def _parse_connected_clients_list(html): lst = ConnectedClientsList() regex = re.compile('dhcpd_lease = \[ (.+?)\];\nlist = \[') match_object = regex.search(html) if match_object is None: return lst def parse_lease_time(lease): regex = '(\d+) days?, (\d+):(\d+):(\d+)' match_object = re.compile(regex).search(lease) if match_object is None: return ConnectedClientsListItem.LEASE_TIME_PERMANENT days, hours, minutes, seconds = map(int, match_object.groups()) return days * 86400 + hours * 3600 + minutes * 60 + seconds try: array = ast.literal_eval('[%s]' % match_object.group(1)) for name, ip, mac, lease_time in array: item = ConnectedClientsListItem() item.set_client_name(name) item.set_ip(ip) item.set_mac(converter.normalize_mac(mac)) item.set_lease_time(parse_lease_time(lease_time)) lst.append(item) return lst except ValueError: raise RouterParseError('Cannot parse connected clients!')
def _parse_connected_clients_list(html): lst = ConnectedClientsList() regex = re.compile('dhcpd_lease = \[ (.+?)\];\nlist = \[') match_object = regex.search(html) if match_object is None: return lst def parse_lease_time(lease): regex = '(\d+) days?, (\d+):(\d+):(\d+)' match_object = re.compile(regex).search(lease) if match_object is None: return ConnectedClientsListItem.__class__.LEASE_TIME_PERMANENT days, hours, minutes, seconds = map(int, match_object.groups()) return days * 86400 + hours * 3600 + minutes * 60 + seconds try: array = ast.literal_eval('[%s]' % match_object.group(1)) for name, ip, mac, lease_time in array: item = ConnectedClientsListItem() item.set_client_name(name) item.set_ip(ip) item.set_mac(converter.normalize_mac(mac)) item.set_lease_time(parse_lease_time(lease_time)) lst.append(item) return lst except ValueError: raise RouterParseError('Cannot parse connected clients!')
def parse_connected_clients_list(html): lst = ConnectedClientsList() regex = re.compile('var x=new Array\((.+?)\);') match_object = regex.search(html) if match_object is None: raise RouterParseError('Cannot parse connected clients list') match_str = match_object.group(1) if match_str == '""': return lst import ast try: list_literal = '[%s]' % match_str list_items = ast.literal_eval(list_literal) list_items.pop() # empty 'sentinel' string - not needed list_items = [ v.decode('utf-8', 'ignore') if isinstance(v, bytes) else v for v in list_items ] for ip, name, mac in split_in_groups(list_items, 3): item = ConnectedClientsListItem() item.set_client_name(name) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(item.__class__.LEASE_TIME_PERMANENT) lst.append(item) return lst except Exception: return lst
def _parse_connected_clients_list(html): lst = ConnectedClientsList() regex = re.compile('<tr bgcolor=#(?:.+?)><td><font size=2>(.+?)</td>' '<td><font size=2>(.+?)</td>' '<td><font size=2>([0-9]+)</td></tr>') for i, (ip, mac, time) in enumerate(regex.findall(html)): item = ConnectedClientsListItem() item.set_client_name('Client %d' % (i + 1)) item.set_ip(ip) item.set_mac(converter.normalize_mac(mac)) item.set_lease_time(int(time)) lst.append(item) return lst
def _parse_connected_clients_list(html): lst = ConnectedClientsList() regex = "<tr>(?:.+?)<span class=\"ttext\">(.+?)</span>(?:.+?)<span class=\"ttext\">(.+?)</span>(?:.+?)<span class=\"ttext\">(.+?)</span>(?:.+?)</tr>" for ip, name, mac in re.compile(regex, re.DOTALL).findall(html): if ip == '--': # I've seen such entries on WGR614v7 only.. let's ignore them continue if not validator.is_valid_ip_address(ip): raise RouterParseError('Invalid IP address: %s' % ip) item = ConnectedClientsListItem() item.set_client_name(name) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(None) # no lease time information available lst.append(item) return lst
def _parse_connected_clients_list(html): dhcp_list = re.compile('var dhcpList=new Array\((.*)\);').search(html) if dhcp_list is None: raise RouterParseError('Cannot find DHCP list.') dhcp_list = dhcp_list.group(1) results = re.compile("'(.+?);(.+?);(.+?);[01];(\d+)'").findall(dhcp_list) lst = ConnectedClientsList() for client_name, ip, mac, lease_time in results: if not validator.is_valid_ip_address(ip): raise RouterParseError('Invalid IP address: %s' % ip) if not validator.is_valid_mac_address(mac): raise RouterParseError('Invalid MAC address: %s' % mac) item = ConnectedClientsListItem() item.set_client_name(client_name) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(int(lease_time)) lst.append(item) return lst
def _parse_connected_clients_list(html): regex_dhcp_list = "<tr class=table2 align=center><td><font size=2>(.+?)</td><td><font size=2>(.+?)</td>" lst = ConnectedClientsList() for id, (ip, mac) in enumerate(re.findall(regex_dhcp_list, html), start=1): if ip == "None": # this entry is added when there are no connected clients break if not validator.is_valid_ip_address(ip): raise RouterParseError('Invalid IP address: %s' % ip) if not validator.is_valid_mac_address(mac): raise RouterParseError('Invalid MAC address: %s' % mac) item = ConnectedClientsListItem() item.set_client_name('Client %d' % id) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) item.set_lease_time(0) lst.append(item) return lst
def _parse_connected_clients_list(html): # the last 2 elements of the data array are not needed result = _extract_js_array_data(html, 'DHCPDynList')[:-2] lst = ConnectedClientsList() # each 4 subsequent items are related (client_name, mac_address, ip, lease_time) for client_name, mac, ip, lease_time in split_in_groups(result, 4): if not validator.is_valid_ip_address(ip): raise RouterParseError('Invalid IP address: %s' % ip) item = ConnectedClientsListItem() item.set_client_name(client_name) item.set_mac(converter.normalize_mac(mac)) item.set_ip(ip) if lease_time == 'Permanent': item.set_lease_time(item.__class__.LEASE_TIME_PERMANENT) else: item.set_lease_time(_parse_lease_time(lease_time)) lst.append(item) return lst