Skip to content

Instantly share code, notes, and snippets.

@dirkakrid
Forked from jeffbrl/namespace.py
Created February 14, 2017 03:37
Show Gist options
  • Save dirkakrid/e25243cd5ddbb40f9360d82b8674cb09 to your computer and use it in GitHub Desktop.
Save dirkakrid/e25243cd5ddbb40f9360d82b8674cb09 to your computer and use it in GitHub Desktop.
Using pyez's remove_namespaces function to strip namespace information
from lxml import etree
from jnpr.junos.jxml import remove_namespaces
# file contains output of 'show interfaces ge-0/0/0.0'
fh = open('router_output2.xml', 'r')
parser = etree.XMLParser(remove_blank_text=True)
tree = etree.parse(fh, parser)
tree = remove_namespaces(tree)
xpath = "//logical-interface/address-family[address-family-name = 'inet']/interface-address/ifa-local"
tree_list = tree.xpath(xpath)
if tree_list:
print tree_list[0].text.strip()
else:
print "xpath expression did not match"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment