Automation

last person joined: yesterday 

Ask questions and share experiences about Apstra, Paragon, and all things network automation.
  • 1.  XML processing with Python lxml

    Posted 01-25-2016 11:15

    I'm having some difficulty processing Junos XML with the lxml module with Python.  Much of the difficulty lies with the namespace that appends a URI to the tag names.  With 'find' at least there is a fairly easy workaround with {*}, such as:

     

    root.find('{*}chassis')

     

    but attempting to use xpath expressions to pull out elemts has not worked at all so far for me.  I noticed that the PyEZ modulse makes use of lxml, so I was wondering if there was anyone with experience using that module to process junos xml.



  • 2.  RE: XML processing with Python lxml
    Best Answer

     
    Posted 01-26-2016 09:07

    Hi,

     

    Hopefully the following might help to demonstrate.

     

    In this example the entire configuration has been retrieved from the device, and using the xpath() method just a subset of that data has been output (part of the global address-book in this scenario).

    from jnpr.junos import Device
    from lxml import etree
    dev = Device(host='x.x.x.x', user='foo', passwd='bar')
    dev.open()
    data = dev.rpc.get_config()
    output = data.xpath('/rpc-reply/configuration/security/address-book[name="global"]/address[name="MySlashTwenty"]')
    print etree.tostring(output[0])

    An alternate approach could also be to only retrieve a subset of the data from the device instead of everything,

    from jnpr.junos import Device
    from lxml import etree
    from lxml.builder import E
    dev = Device(host='x.x.x.x', user='foo', passwd='bar')
    dev.open()
    filter = E('security', E('address-book', E('name', 'global'), E('address', E('name','GoogleDNS'))))
    data = dev.rpc.get_config(filter)
    output = data.xpath('/rpc-reply/configuration')
    print etree.tostring(output[0])
    

    The results produced for each scenario are as shown below:

     

    Example 1:
    <address>
                    <name>MySlashTwenty</name>
                    <ip-prefix>10.142.96.0/20</ip-prefix>
    </address>
     
    
    Example 2:
    <configuration changed-seconds="1453826277" changed-localtime="2016-01-26 16:37:57 UTC">
        <security>
            <address-book>
                <name>global</name>
                <address>
                    <name>GoogleDNS</name>
                    <ip-prefix>4.4.4.4/32</ip-prefix>
                </address>
            </address-book>
        </security>
    </configuration>

    I have to admit, that I normally develop scripts in SLAX and my Python is far from great, and there could of course be better ways to perform this...


    Regards,

    Andy

     



  • 3.  RE: XML processing with Python lxml

    Posted 03-18-2016 08:52

    Thanks asharp, I'm not quite sure why, but it does seem if you get the entire config the namespace issue isn't as much of a concern, maybe something to do with the sub elements not needing the namespace attached.