"""AMT's own network ports (``AMT_EthernetPortSettings``). AMT shares the machine's network controller: the wired port has the host's MAC address and, in DHCP mode, the host's IP address too. A port without a MAC address is a wireless port on a machine without a WLAN card. """ from __future__ import annotations import ipaddress from typing import Any from napalm_amt.parsers.common import is_true, text _Instance = dict[str, Any] _WIRELESS = "3" # PhysicalConnectionType: 0 integrated LAN, 1 discrete LAN, 2 dock, 3 WLAN def _mac(value: str) -> str: mac = value.replace("-", ":").lower() return "" if not mac.replace(":", "").strip("0") else mac def _prefix(mask: str) -> int | None: if not mask: return None try: return ipaddress.IPv4Network(f"0.0.0.0/{mask}").prefixlen except ValueError: return None def parse_ethernet_ports(ports: list[_Instance]) -> list[dict[str, Any]]: """The ports in use: name (``wired``/``wireless``), MAC, link, DHCP, whether AMT shares the host's IP address, and its own address where AMT reports one.""" parsed: list[dict[str, Any]] = [] for port in ports: mac = _mac(text(port.get("MACAddress"))) if not mac: continue name = "wireless" if text(port.get("PhysicalConnectionType")) == _WIRELESS else "wired" if any(p["name"] == name for p in parsed): name = f"{name}{len(parsed)}" dhcp = is_true(port.get("DHCPEnabled")) ip = text(port.get("IPAddress")) parsed.append( { "name": name, "mac_address": mac, "link_up": is_true(port.get("LinkIsUp")), "dhcp": dhcp, "shared_ip": is_true(port.get("SharedDynamicIP" if dhcp else "SharedStaticIp")), "ip": ip, "prefix_length": _prefix(text(port.get("SubnetMask"))) if ip else None, } ) return parsed