summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorA. Wilcox <AWilcox@Wilcox-Tech.com>2020-12-11 07:09:42 -0600
committerA. Wilcox <AWilcox@Wilcox-Tech.com>2020-12-11 07:09:42 -0600
commitcb0dee93926f7658560f84496225b1efd80b5035 (patch)
tree48f6f81896f2c11cdcbe27762aa44781ceee1307
parent727609f423cdeb6f198aebbea89c957ce72f88c4 (diff)
downloadnetconfapk-cb0dee93926f7658560f84496225b1efd80b5035.tar.gz
netconfapk-cb0dee93926f7658560f84496225b1efd80b5035.tar.bz2
netconfapk-cb0dee93926f7658560f84496225b1efd80b5035.tar.xz
netconfapk-cb0dee93926f7658560f84496225b1efd80b5035.zip
ifupdown-ng NMSA: Add live_addresses method
-rw-r--r--ncserver/module/nms_ifupdownng.py26
1 files changed, 26 insertions, 0 deletions
diff --git a/ncserver/module/nms_ifupdownng.py b/ncserver/module/nms_ifupdownng.py
index 1142f31..7c1746e 100644
--- a/ncserver/module/nms_ifupdownng.py
+++ b/ncserver/module/nms_ifupdownng.py
@@ -12,7 +12,9 @@ SPDX-License-Identifier: NCSA
from contextlib import contextmanager
+import ipaddress
import logging
+import netifaces
import pathlib
import socket
import subprocess
@@ -684,6 +686,30 @@ def list_addresses(iface: str) -> list:
return _find_many(iface, 'address')
+def live_addresses(iface: str) -> list:
+ """Retrieve live addresses for the specified interface."""
+ addresses = list()
+
+ if iface not in netifaces.interfaces():
+ LOGGER.warning(_("interface %s is not live"), iface)
+ return addresses
+
+ raw = netifaces.ifaddresses(iface)
+ for v4 in raw.get(socket.AF_INET, tuple()):
+ addr = v4['addr']
+ mask = v4['netmask']
+ iface = ipaddress.IPv4Interface("{a}/{m}".format(a=addr, m=mask))
+ addresses.append(iface)
+
+ for v6 in raw.get(socket.AF_INET6, tuple()):
+ addr = v6['addr'].split('%')[0]
+ mask = v6['netmask'][v6['netmask'].find('/') + 1:]
+ iface = ipaddress.IPv6Interface("{a}/{m}".format(a=addr, m=mask))
+ addresses.append(iface)
+
+ return addresses
+
+
def add_address(iface: str, _type, addr: str, prefix):
"""Add an address of the specified ``type`` to the specified interface."""
_load_config()