Skip to content
Snippets Groups Projects

Fixes after consul-setup that work with existing integration tests

Merged Jan David Mol requested to merge consul-setup-tangostationcontrol-fixes into consul-setup

Files

# Copyright (C) 2024 ASTRON (Netherlands Institute for Radio Astronomy)
# Copyright (C) 2024 ASTRON (Netherlands Institute for Radio Astronomy)
# SPDX-License-Identifier: Apache-2.0
# SPDX-License-Identifier: Apache-2.0
import netifaces
import psutil
 
from ipaddress import IPv4Address
 
from socket import AddressFamily
 
 
__all__ = ["get_mac", "get_ip"]
 
 
 
def _get_interface_addresses(interface: str) -> list:
 
try:
 
return psutil.net_if_addrs()[interface]
 
except KeyError as e:
 
raise ValueError(f"Cannot find interface {interface}") from e
def get_mac(interface: str) -> str:
def get_mac(interface: str) -> str:
"""Returns the MAC address of the given interface (f.e. 'eth0')."""
"""Returns the MAC address of the given interface (f.e. 'eth0')."""
try:
for address in _get_interface_addresses(interface):
addresses = netifaces.ifaddresses(interface)
if address.family == AddressFamily.AF_PACKET:
return addresses[netifaces.AF_LINK][0]["addr"]
return address.address
except (ValueError, IndexError, KeyError) as e:
raise ValueError(
f"Could not obtain MAC address of interface {interface}"
) from e
 
raise ValueError(f"Cannot obtain MAC address of interface {interface}")
def get_ip(interface: str) -> str:
"""Returns the IP address of the given interface (f.e. 'eth0')."""
try:
def get_ip(interface: str) -> IPv4Address:
addresses = netifaces.ifaddresses(interface)
"""Returns the IPv4 address of the given interface (f.e. 'eth0')."""
return addresses[netifaces.AF_INET][0]["addr"]
except (ValueError, IndexError, KeyError) as e:
for address in _get_interface_addresses(interface):
raise ValueError(f"Could not obtain IP address of interface {interface}") from e
if address.family == AddressFamily.AF_INET:
 
return IPv4Address(address.address)
 
 
raise ValueError(f"Cannot obtain IP address of interface {interface}")
Loading