test nic state link api
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from typing import List, Dict, Optional
|
||||
from typing import List, Optional
|
||||
import logging
|
||||
import os
|
||||
|
||||
@@ -30,6 +30,49 @@ def check_interface_up(iface: str) -> bool:
|
||||
return False
|
||||
|
||||
|
||||
def _read_sysfs_text(path: str) -> Optional[str]:
|
||||
"""Read one sysfs file and return stripped text, or `None` if unavailable."""
|
||||
try:
|
||||
with open(path, "r") as f:
|
||||
return f.read().strip()
|
||||
except FileNotFoundError:
|
||||
return None
|
||||
except Exception:
|
||||
logger.exception("Error reading sysfs path %s", path)
|
||||
return None
|
||||
|
||||
|
||||
def read_interface_operstate(iface: str) -> Optional[str]:
|
||||
"""Return the kernel operstate string for an interface."""
|
||||
return _read_sysfs_text(f"/sys/class/net/{iface}/operstate")
|
||||
|
||||
|
||||
def read_interface_carrier(iface: str) -> Optional[bool]:
|
||||
"""Return the carrier state for an interface if available."""
|
||||
value = _read_sysfs_text(f"/sys/class/net/{iface}/carrier")
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
return int(value, 10) == 1
|
||||
except ValueError:
|
||||
logger.warning("Unexpected carrier value for %s: %r", iface, value)
|
||||
return None
|
||||
|
||||
|
||||
def read_interface_admin_up(iface: str) -> Optional[bool]:
|
||||
"""Return whether the interface has the IFF_UP flag set."""
|
||||
value = _read_sysfs_text(f"/sys/class/net/{iface}/flags")
|
||||
if value is None:
|
||||
return None
|
||||
|
||||
try:
|
||||
return bool(int(value, 0) & 0x1)
|
||||
except ValueError:
|
||||
logger.warning("Unexpected flags value for %s: %r", iface, value)
|
||||
return None
|
||||
|
||||
|
||||
def _read_bridge_ports_from_sysfs(bridge: str) -> List[str]:
|
||||
"""
|
||||
Read bridge member interfaces from sysfs. Internal helper that always reads.
|
||||
|
||||
Reference in New Issue
Block a user