pyez_test.py 1.85 KiB
# import os
from jnpr.junos import Device
# from inventory_provider import config
from lxml import etree
def load_config(hostname, ssh_params):
dev = Device(
host=hostname,
user=ssh_params['username'],
ssh_private_key_file=ssh_params['private-key'])
dev.open()
# data = dev.rpc.get_config(options={'format': 'json'})
return dev.rpc.get_config()
def load_interfaces(hostname, ssh_params):
data = load_config(hostname, ssh_params)
# print(etree.tostring(data, encoding='unicode'))
def _ifc_info(e):
name = e.find('name')
assert name is not None, "expected interface 'name' child element"
description = e.find('description')
return {
'name': name.text,
'description': description.text if description is not None else None
}
# for i in data.xpath('//configuration/groups/interfaces/interface'):
for i in data.xpath('//configuration/interfaces/interface'):
info = _ifc_info(i)
yield info
# ifc_name = i.xpath('./name/text()')
# assert ifc_name
# print(ifc_name[0])
for u in i.xpath('./unit'):
unit_info = _ifc_info(u)
unit_info['name'] = "%s.%s" % (info['name'], unit_info['name'])
yield unit_info
# print("%s.%s" % (ifc_name[0], u))
#
# print("-----routing-instances-----")
# for i in data.xpath("//configuration/routing-instances/instance/interface"):
# print(i.xpath('./name/text()'))
# if __name__ == "__main__":
# with open("config.json") as f:
# params = config.load(f)
# for r in params['routers']:
# netconf = _load_netconf(r['hostname'], params['ssh'])
# with open(os.path.join(TEST_DATA_DIR, "%s-netconf.xml" % r['hostname']), 'w') as f:
# f.write(etree.tostring(netconf, encoding='unicode'))