File: ip_address.py

package info (click to toggle)
python-asusrouter 1.21.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,856 kB
  • sloc: python: 20,497; makefile: 3
file content (46 lines) | stat: -rw-r--r-- 1,159 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
"""IP address module."""

from enum import Enum
import re

from asusrouter.tools.converters import clean_string


class IPAddressType(str, Enum):
    """IP address type class."""

    DHCP = "dhcp"
    PPPOE = "pppoe"
    PPTP = "pptp"
    STATIC = "static"
    UNKNOWN = "unknown"


def read_ip_address_type(data: str | None) -> IPAddressType:
    """Read IP address type from data string."""

    data = clean_string(data)

    if data:
        data = data.lower()

    # Find the IP address type in the IPAddressType enum by value
    for ip_address_type in IPAddressType:
        if ip_address_type.value == data:
            return ip_address_type

    # Some other values to convert
    if data == "manual":
        return IPAddressType.STATIC

    # If the IP address type is not found, return the UNKNOWN type
    return IPAddressType.UNKNOWN


def read_dns_ip_address(data: str) -> list[str]:
    """Read DNS record and return the list of IP addresses."""

    # Regex to find each IP address in a string of IP addresses
    regex = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
    # Find all IP addresses in the string
    return re.findall(regex, data)