Last active
July 12, 2016 14:01
-
-
Save jeffbrl/f60daf9af3082bb2aaacb9781753ecf3 to your computer and use it in GitHub Desktop.
Parsing Junos XML output using jxmlease examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import jxmlease | |
| # Accessing current time from get-system-uptime | |
| with open('get-system-uptime.xml') as xml: | |
| root = jxmlease.parse(xml) | |
| uptime = root['rpc-reply']['system-uptime-information']['current-time']['date-time'].get_cdata() | |
| print("uptime: {}".format(uptime)) | |
| print('\n===============\n') | |
| # List all software package names and comments from full xml document using XMLListNode | |
| with open('get-software-information.xml') as xml: | |
| root = jxmlease.parse(xml) | |
| for node in root['rpc-reply']['software-information']['package-information']: | |
| name = node['name'].get_cdata() | |
| comment = node['comment'].get_cdata() | |
| print("package name: {} comment: {}".format(name, comment)) | |
| print('\n===============\n') | |
| # List selected information (software package names and comments again) using a generator | |
| with open('get-software-information.xml') as xml: | |
| # underscores can be used below as path and match are unused in this example | |
| for path, match, node in jxmlease.parse(xml, generator='package-information'): | |
| name = node['name'].get_cdata() | |
| comment = node['comment'].get_cdata() | |
| print("package name: {} comment: {}".format(name, comment)) | |
| print('\n===============\n') | |
| # List package-name names and comments with XMLDict's find_nodes_with_tag method | |
| with open('get-software-information.xml') as xml: | |
| root = jxmlease.parse(xml) | |
| for node in root.find_nodes_with_tag('package-information'): | |
| name = node['name'].get_cdata() | |
| comment = node['comment'].get_cdata() | |
| print("package name: {} comment: {}".format(name, comment)) | |
| print('\n===============\n') | |
| # Accessing Junos version in 14.2+ using jdict() | |
| with open('get-software-information.xml') as xml: | |
| root = jxmlease.parse(xml) | |
| version = root['rpc-reply']['software-information']['package-information'].jdict()['junos-version']['comment'].get_cdata() | |
| print("version: {}".format(version)) | |
| print('\n===============\n') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment