Skip to content
Snippets Groups Projects
Commit 3e682d0e authored by Erik Reid's avatar Erik Reid
Browse files

added utility for extracting a relevant snmp community string

parent c894a0d6
No related branches found
No related tags found
No related merge requests found
......@@ -339,3 +339,13 @@ def local_interfaces(
mask = a['netmask']
yield ipaddress.ip_interface('%s/%s' % (addr, mask))
def snmp_community_string(netconf_config):
my_addressess = list([i.ip for i in local_interfaces()])
for community in netconf_config.xpath('//configuration/snmp/community'):
for subnet in community.xpath('./clients/name/text()'):
allowed_network = ipaddress.ip_network(subnet, strict=False)
for me in my_addressess:
if me in allowed_network:
return community.xpath('./name/text()')[0]
return None
......@@ -98,3 +98,20 @@ def test_bgp_list(netconf_doc):
routes = list(juniper.list_bgp_routes(netconf_doc))
jsonschema.validate(routes, schema)
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}]},
'eth0': {18: [{'addr': '78:4f:43:76:73:ba'}],
2: [{'addr': '83.97.92.239', 'netmask': '255.255.252.0', 'broadcast': '83.97.95.255'}],
30: [{'addr': 'fe80::250:56ff:fea1:8340', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1024},
{'addr': '2001:798:3::104', 'netmask': 'ffff:ffff:ffff:ffff::/64', 'flags': 1088}]}
}
def test_snmp_community_string(mocker, netconf_doc):
mocker.patch('netifaces.interfaces', lambda: NETIFACES_TEST_DATA.keys())
mocker.patch('netifaces.ifaddresses', lambda n: NETIFACES_TEST_DATA[n])
assert juniper.snmp_community_string(netconf_doc) == '0pBiFbD'
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment