From d8b84699a5cfbb0c75a60634fcd7007638f6cedd Mon Sep 17 00:00:00 2001 From: Thomas Davis Date: Wed, 28 Aug 2019 08:24:25 -0700 Subject: [PATCH] fixes the IPv6 subnet mask problem, plus default values if no IPv4 or IPv6 address is defined. --- netbox_agent/network.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/netbox_agent/network.py b/netbox_agent/network.py index 786c642..2ae5641 100644 --- a/netbox_agent/network.py +++ b/netbox_agent/network.py @@ -60,10 +60,18 @@ class Network(): continue ip_addr = netifaces.ifaddresses(interface).get(netifaces.AF_INET) - try: - ip_addr += netifaces.ifaddresses(interface).get(netifaces.AF_INET6) - except: - logging.debug('No IPv6 addresses defined.') + ip6_addr += netifaces.ifaddresses(interface).get(netifaces.AF_INET6) + + # netifaces returns a ipv6 netmask that netaddr does not understand. + # this strips the netmask down to the correct format for netaddr. + # ie, this: + # {'addr': 'fe80::ec4:7aff:fe59:ec4a%eno1.50', 'netmask': 'ffff:ffff:ffff:ffff::/64'} + # becomes: + # {'addr': 'fe80::ec4:7aff:fe59:ec4a%eno1.50', 'netmask': 'ffff:ffff:ffff:ffff::'} + # + for addr in ip6_addr: + addr["netmask"] = re.sub("/\d+$", "", addr["netmask"]) + ip_addr.append(addr) if NETWORK_IGNORE_IPS and ip_addr: for i, ip in enumerate(ip_addr):