diff --git a/inventory_provider/juniper.py b/inventory_provider/juniper.py index 8dca8768b1e0ec76a1fc9968a5fa5ef85eddc70e..93adf75a5703a71ee0092e059dea480758d55b8f 100644 --- a/inventory_provider/juniper.py +++ b/inventory_provider/juniper.py @@ -1,8 +1,10 @@ import logging import re +import ipaddress from jnpr.junos import Device from lxml import etree +import netifaces import requests from requests.auth import HTTPBasicAuth @@ -314,3 +316,26 @@ def load_routers_from_junosspace(config): "name": name, "hostname": hostname } + + +def local_interfaces( + type=netifaces.AF_INET, + omit_link_local=True, + omit_loopback=True): + for n in netifaces.interfaces(): + if omit_loopback and re.match(r'^lo\d+', n): + continue + am = netifaces.ifaddresses(n) + for a in am.get(type, []): + if omit_link_local and a['addr'].startswith('fe80:'): + continue + m = re.match(r'^(.+?)(%.*)?$', a['addr']) + assert m + addr = m.group(1) + m = re.match(r'.*/(\d+)$', a['netmask']) + if m: + mask = m.group(1) + else: + mask = a['netmask'] + yield ipaddress.ip_interface('%s/%s' % (addr, mask)) + diff --git a/test/test_juniper_data.py b/test/test_juniper_data.py new file mode 100644 index 0000000000000000000000000000000000000000..9ba433f9005a8cc02bbfe3c3f54f9f44b3c31987 --- /dev/null +++ b/test/test_juniper_data.py @@ -0,0 +1,59 @@ +import netifaces +import ipaddress + +from inventory_provider import juniper + +NETIFACES_TEST_DATA = { + 'lo0': {2: [{'addr': '127.0.0.1', 'netmask': '255.0.0.0', 'peer': '127.0.0.1'}], + 30: [{'addr': '::1', 'netmask': 'ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff/128', 'peer': '::1', 'flags': 0}, + {'addr': 'fe80::1%lo0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]}, + 'gif0': {}, + 'stf0': {}, + 'XHC20': {}, + 'XHC4': {}, + 'XHC3': {}, + 'en3': {18: [{'addr': 'b6:00:24:b9:f0:01'}]}, + 'en8': {18: [{'addr': 'b6:00:24:b9:f0:00'}]}, + 'en4': {18: [{'addr': 'b6:00:24:b9:f0:05'}]}, + 'en9': {18: [{'addr': 'b6:00:24:b9:f0:04'}]}, + 'en0': {18: [{'addr': '78:4f:43:76:73:ba'}], + 2: [{'addr': '195.169.24.149', 'netmask': '255.255.255.128', 'broadcast': '195.169.24.255'}], + 30: [{'addr': 'fe80::1c97:ec77:3f32:cdfe%en0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024}, + {'addr': '2001:610:9d8:4:4d7:f763:9815:e78d', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088}, + {'addr': '2001:610:9d8:4:492e:61b6:2c92:c387', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 192}]}, + 'p2p0': {18: [{'addr': '0a:4f:43:76:73:ba'}]}, + 'awdl0': {18: [{'addr': '8e:87:e3:bb:92:1f'}], + 30: [{'addr': 'fe80::8c87:e3ff:febb:921f%awdl0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]}, + 'bridge0': {18: [{'addr': 'b6:00:24:b9:f0:01'}]}, + 'utun0': {30: [{'addr': 'fe80::8328:d0ef:52b4:d379%utun0', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]}, + 'utun1': {30: [{'addr': 'fe80::5a75:c789:2fa0:6ee4%utun1', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]}, + 'XHC0': {}, + 'XHC1': {}, + 'XHC2': {}, + 'en21': {18: [{'addr': '64:4b:f0:10:23:25'}], + 2: [{'addr': '195.169.24.170', 'netmask': '255.255.255.128', 'broadcast': '195.169.24.255'}], + 30: [{'addr': 'fe80::41c:798c:3fff:f8c9%en21', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024}, + {'addr': '2001:610:9d8:4:c1e:4402:e7cf:547f', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088}, + {'addr': '2001:610:9d8:4:911c:954d:d4e2:baef', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 192}]}, + 'en5': {18: [{'addr': 'ac:de:48:00:11:22'}], + 30: [{'addr': 'fe80::aede:48ff:fe00:1122%en5', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 0}]}, + 'en18': {18: [{'addr': 'ca:3c:85:86:34:2a'}], 2: [{'addr': '169.254.184.83', 'netmask': '255.255.0.0'}]}, +} + + +def test_local_v4_interfaces(mocker): + mocker.patch('netifaces.interfaces', lambda: NETIFACES_TEST_DATA.keys()) + mocker.patch('netifaces.ifaddresses', lambda n: NETIFACES_TEST_DATA[n]) + addresses = list(juniper.local_interfaces()) + assert len(addresses) == 3 + for a in addresses: + assert isinstance(a, ipaddress.IPv4Interface) + + +def test_local_v6_interfaces(mocker): + mocker.patch('netifaces.interfaces', lambda: NETIFACES_TEST_DATA.keys()) + mocker.patch('netifaces.ifaddresses', lambda n: NETIFACES_TEST_DATA[n]) + addresses = list(juniper.local_interfaces(netifaces.AF_INET6)) + assert len(addresses) == 4 + for a in addresses: + assert isinstance(a, ipaddress.IPv6Interface)