Esempio n. 1
0
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
Esempio n. 2
0
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
Esempio n. 3
0
def _parse_addr_reservation_list(contents):
    array_name = 'dhcpList'

    result = _extract_js_array_data(contents, array_name)
    result = result[:-2]    # the last 2 elements are not needed

    # each 3 subsequent items are related (mac_address, ip, is_enabled)
    list_raw = split_in_groups(result, 3)

    reservation_list = DHCPReservationList()
    for mac, ip, is_enabled in list_raw:
        item = DHCPReservationListItem()
        item.set_mac(converter.normalize_mac(mac))
        item.set_ip(ip)
        item.set_enabled_status(is_enabled == 1)
        reservation_list.append(item)

    return reservation_list
Esempio n. 4
0
def _parse_addr_reservation_list(contents):
    array_name = 'dhcpList'

    result = _extract_js_array_data(contents, array_name)
    result = result[:-2]    # the last 2 elements are not needed

    # each 3 subsequent items are related (mac_address, ip, is_enabled)
    list_raw = split_in_groups(result, 3)

    reservation_list = DHCPReservationList()
    for mac, ip, is_enabled in list_raw:
        item = DHCPReservationListItem()
        item.set_mac(converter.normalize_mac(mac))
        item.set_ip(ip)
        item.set_enabled_status(is_enabled == 1)
        reservation_list.append(item)

    return reservation_list
Esempio n. 5
0
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
Esempio n. 6
0
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
Esempio n. 7
0
 def denormalize_mac(mac):
     # abcdef123456 => AB:CD:EF:12:34:45
     byte_octets = split_in_groups(mac, 2)
     return (':'.join(byte_octets)).upper()