Lab-7:Python for network automation – XML parsing

Knowledge to XML parsing is important for network automation, these days  many modern interfaces generate XML formatted output. I have written a small Python script which parses XML using Minidom and creates Python dictionary. You can download script (parse_route_table.py) from here.

Pre-condition:

  • Juniper srx with NETCONF enabled
    • set system services netconf ssh
  • NETCONF ncclient installed
    • sudo pip install ncclient

Procedure:

Script follows these steps

  • Connect to srx using NETCONF
  • Generate ‘show route | display xml’ in xml format
  • Parse xml and generate a Python dictionary for destination and outgoing interface.

Output

sjakhwal@rtxl3rld05:~/scripts$ python parse_routing_table.py
=== Route table name =inet.0, Number of active routes =9 ====
destination-ip=0.0.0.0/0,via=ge-0/0/0.0
destination-ip=11.11.11.0/24,via=ge-0/0/1.0
destination-ip=11.11.11.1/32,via=ge-0/0/1.0
destination-ip=192.168.47.5/32,via=lo0.0
destination-ip=192.168.47.6/32,via=lo0.0
destination-ip=192.168.47.7/32,via=lo0.0
destination-ip=192.168.47.8/32,via=lo0.0
destination-ip=192.168.122.0/24,via=ge-0/0/0.0
destination-ip=192.168.122.35/32,via=ge-0/0/0.0

 

Note: Below sample XML
root> show route | display xml
<rpc-reply xmlns:junos=”http://xml.juniper.net/junos/12.1X47/junos”&gt;
<route-information xmlns=”http://xml.juniper.net/junos/12.1X47/junos-routing”&gt;
<!– keepalive –>
<route-table>
<table-name>inet.0</table-name>
<destination-count>9</destination-count>
<total-route-count>9</total-route-count>
<active-route-count>9</active-route-count>
<holddown-route-count>0</holddown-route-count>
<hidden-route-count>0</hidden-route-count>
<rt junos:style=”brief”>
<rt-destination>0.0.0.0/0</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Access-internal</protocol-name>
<preference>12</preference>
<age junos:seconds=”1096554″>1w5d 16:35:54</age>
<nh>
<selected-next-hop/>
<to>192.168.122.1</to>
<via>ge-0/0/0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>11.11.11.0/24</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”686008″>1w0d 22:33:28</age>
<nh>
<selected-next-hop/>
<via>ge-0/0/1.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>11.11.11.1/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Local</protocol-name>
<preference>0</preference>
<age junos:seconds=”686008″>1w0d 22:33:28</age>
<nh-type>Local</nh-type>
<nh>
<nh-local-interface>ge-0/0/1.0</nh-local-interface>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.47.5/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096716″>1w5d 16:38:36</age>
<nh>
<selected-next-hop/>
<via>lo0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.47.6/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096716″>1w5d 16:38:36</age>
<nh>
<selected-next-hop/>
<via>lo0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.47.7/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096716″>1w5d 16:38:36</age>
<nh>
<selected-next-hop/>
<via>lo0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.47.8/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096716″>1w5d 16:38:36</age>
<nh>
<selected-next-hop/>
<via>lo0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.122.0/24</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Direct</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096554″>1w5d 16:35:54</age>
<nh>
<selected-next-hop/>
<via>ge-0/0/0.0</via>
</nh>
</rt-entry>
</rt>
<rt junos:style=”brief”>
<rt-destination>192.168.122.35/32</rt-destination>
<rt-entry>
<active-tag>*</active-tag>
<current-active/>
<last-active/>
<protocol-name>Local</protocol-name>
<preference>0</preference>
<age junos:seconds=”1096554″>1w5d 16:35:54</age>
<nh-type>Local</nh-type>
<nh>
<nh-local-interface>ge-0/0/0.0</nh-local-interface>
</nh>
</rt-entry>
</rt>
</route-table>
</route-information>
<cli>
<banner></banner>
</cli>
</rpc-reply>

One thought on “Lab-7:Python for network automation – XML parsing

Leave a comment