[python]Convert show ip int brief into structured json data

Cisco IOS commands output is unstructured in the perception of scripting, the output format made sense only to engineers, however if you need to program such output is difficult if the output is not processed.
One of the better way to structure the unstructured cisco output is to check each row of the output, then for each item assign to its respective description in a dictionary format.

Screenshot 2019-08-27 at 8.34.36 PM
To scripting this is unstructured data, the entire output is a block of string to the sense of scripting. Hence a need to process this output into structured data for later manipulation.

from netmiko import ConnectHandler
import re
import json
from pprint import pprint

ios = {
    'device_type': 'cisco_ios', # a structure inventory with ip address
    'ip': '192.168.1.26', # should be used to better check the output.
    'username': 'cisco', # username and password should be stored else where
    'password': 'cisco' # and not in the code.
}
# this regex is to match for gigabit, ethernet, fastethernet and loopback.
intf_pattern = "^[lLgGeEfF]\S+[0-9]/?[0-9]*"
# create a regex object with the pattern in place.
regex = re.compile(intf_pattern)
# initialize this list to collect interface information.
interfaces = []
# create a connection object to send commands
conn = ConnectHandler(**ios)
# send the show ip int brief command for the output.
output = conn.send_command("show ip int brief")
# Start to check for each row, splitlines splits
# until the end of the row, for each row is a list item.
for row in output.splitlines():
# check for interface names only
    if regex.search(row):
# start collecting the dictionary
        interfaces.append(
            {'interface': row.split()[0],
             'ip_address': row.split()[1],
             'ok': row.split()[2],
             'method': row.split()[3],
             'status': row.split()[4],
             'protocol': row.split()[5]}
        )
# Convert into string of dictionary.
pprint(json.dumps(interfaces))

What it can do if the data is structured.
Use case 1 – which interface is up?
Screenshot 2019-08-27 at 8.48.33 PM
This is a command line output, what if you need to do automation and collect the interfaces that have up up status?

With structured data you can do a code like this to leverage the output of the above code.

print([interface.get('interface') for interface in interfaces if interface.get('status') and interface.get('protocol') == 'up'])

The below is the output:
Screenshot 2019-08-27 at 8.50.23 PM

Use case 2 – for the device I need to collect the interface that has assigned ip address and are up
Screenshot 2019-08-27 at 8.52.03 PM
This is the command output, but it is unstructured, supposed you need to store it into csv with a daily script?

You can use something like this:
print([{'interface': interface.get('interface'), 'ip_address': interface.get('ip_address')} for interface in interfaces if (interface.get('status') and interface.get('protocol') == 'up') and interface.get('ip_address') != 'unassigned'])
The output will look like this:
Screenshot 2019-08-27 at 8.58.15 PM

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s