From c77e641c521c03b6098613833fe22e09b11ad23f Mon Sep 17 00:00:00 2001 From: Christian Breunig Date: Sun, 25 Jun 2023 20:09:31 +0200 Subject: bcast-relay: T5313: verify() relay interfaces have IPv4 address configured (cherry picked from commit ca7c063666c038d104082542f04ead6062e79246) --- python/vyos/validate.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'python') diff --git a/python/vyos/validate.py b/python/vyos/validate.py index 9aa23d3dc..83862b722 100644 --- a/python/vyos/validate.py +++ b/python/vyos/validate.py @@ -111,6 +111,24 @@ def is_addr_assigned(addr): return False +def is_afi_configured(interface, afi): + """ Check if given address family is configured, or in other words - an IP + address is assigned to the interface. """ + from netifaces import ifaddresses + from netifaces import AF_INET + from netifaces import AF_INET6 + + if afi not in [AF_INET, AF_INET6]: + raise ValueError('Address family must be in [AF_INET, AF_INET6]') + + try: + addresses = ifaddresses(interface) + except ValueError as e: + print(e) + return False + + return afi in addresses + def is_loopback_addr(addr): """ Check if supplied IPv4/IPv6 address is a loopback address """ from ipaddress import ip_address -- cgit v1.2.3