View Only


This community is currently under full moderation, meaning  all posts will be reviewed before appearing in the community. Please expect a brief delay—there is no need to post multiple times. If your post is rejected, you'll receive an email outlining the reason(s). We've implemented full moderation to control spam. Thank you for your patience and participation.

Expand all | Collapse all

How do I pickout a node from XML replies

This thread has been viewed 11 times
  • 1.  How do I pickout a node from XML replies

    Posted 12-30-2020 06:35

    I'm learning automation, I've set myself the goal of creating a Python script that will take a list of IP addresses and workout out what firewalls zones they are in. 

    ( quite frankly something that should be built into the Junos CLI !! )

    Input IPADDRESS  >> determine interface  > Determine ZONE  > Output ZONE

    What I want to do is really very simple but I'm getting buried in the detail in books, now i fear I cannot see the "wood for the trees"  as a test I have this code

    def findinterface(IPADDRESS):
    print(etree.tostring(dev.rpc.get_route_information(destination=''), encoding='unicode'))


     My question is very simple

    this is the output  how can I isolate 

    so interface=ge-0/0/0.0  something like that 

    How can I in simplest terms pick out the individual data elements I'm interested in ?

    I'm really not a programmer so after the simplest most readable solutions.   

    <route-information xmlns="" xmlns:junos="" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><route-table><table-name>inet.0</table-name>
    <rt style="brief"><rt-destination></rt-destination>
    <age seconds="4375755">7w1d 15:29:15</age>

    Thanks in advance

    Simon Bingham

    Simon Bingham

  • 2.  RE: How do I pickout a node from XML replies

    Posted 12-30-2020 15:06

    I got there in the end,  this is the answer to my own question. 

    from lxml import etree
    from jnpr.junos import Device
    import jxmlease

    dev = Device(host='', user='root', password='XXXXXXX')
    from pprint import pprint

    def findinterface(IPADDRESS):
    result=(etree.tostring(dev.rpc.get_route_information(destination=IPADDRESS), encoding='unicode'))
    xml = jxmlease.parse(result)
    interface = xml['route-information']['route-table']['rt']['rt-entry']['nh']['via']


    Simon Bingham