Automation

 View Only
last person joined: 8 days ago 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
Expand all | Collapse all

How do I pickout a node from XML replies

  • 1.  How do I pickout a node from XML replies

    Posted 12-30-2020 06:35
    Edited by Simon Bingham (technical debt collector) 12-30-2020 11:45

    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='8.8.8.8'), encoding='unicode'))

    interface=findinterface("8.8.8.8")

     My question is very simple

    this is the output  how can I isolate 
    <via>ge-0/0/0.0</via> 

    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="http://xml.juniper.net/junos/12.1X44/junos-routing" xmlns:junos="http://xml.juniper.net/junos/12.1X44/junos" xmlns:nc="urn:ietf:params:xml:ns:netconf:base:1.0"><route-table><table-name>inet.0</table-name>
    <destination-count>3</destination-count>
    <total-route-count>3</total-route-count>
    <active-route-count>3</active-route-count>
    <holddown-route-count>0</holddown-route-count>
    <hidden-route-count>0</hidden-route-count>
    <rt style="brief"><rt-destination>0.0.0.0/0</rt-destination>
    <rt-entry><active-tag>*</active-tag>
    <current-active/>
    <last-active/>
    <protocol-name>Static</protocol-name>
    <preference>5</preference>
    <age seconds="4375755">7w1d 15:29:15</age>
    <nh><selected-next-hop/>
    <to>172.27.233.1</to>
    <via>ge-0/0/0.0</via>
    </nh>
    </rt-entry>
    </rt>
    </route-table>
    </route-information>

    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
    parser=jxmlease.Parser()

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

    dev.open()

    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']
    return(interface)

    interface=findinterface('8.8.8.8')
    print(interface)



    ------------------------------
    Simon Bingham
    ------------------------------