Skip to content

Instantly share code, notes, and snippets.

@einarnn
Created July 26, 2016 21:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save einarnn/b04809db6b9782fb029fd47db40dd3d5 to your computer and use it in GitHub Desktop.
Save einarnn/b04809db6b9782fb029fd47db40dd3d5 to your computer and use it in GitHub Desktop.
Simple use of jxmlease to make XML seem more like JSON
#
# Get running config from an IOS-XR device and print out some
# interface information. Shows how to use the jxmlease library
# for when you'd rather have JSON but have to deal with XML!
#
from ncclient import manager
import jxmlease
HOST = '127.0.0.1'
PORT = 8303
USER = 'cisco'
PASS = 'cisco'
def my_unknown_host_cb(host, fingerprint):
return True
m = manager.connect(host=HOST, port=PORT, username=USER, password=PASS,
timeout=600,
allow_agent=False,
look_for_keys=False,
hostkey_verify=False,
unknown_host_cb=my_unknown_host_cb)
c = m.get_config(source='running')
r = jxmlease.parse(c.data_xml)
for intf in r[u'data'][u'interface-configurations'][u'interface-configuration']:
if 'ipv4-network' in intf.keys():
print("{} has IPv4 address {}".format(
intf[u'interface-name'],
intf[u'ipv4-network'][u'addresses'][u'primary'][u'address']))
else:
print("{} has no IPv4 network config".format(intf[u'interface-name']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment