Beautify your parsed output

Parsed an output from a device and can't make heads or tails of it? This happens when you start looking at parsing data from devices and you're quote new to the world of pyATS, Genie or Python scripting. Outputs from devices are parsed using a specially-made Cisco or community-created scripts called parsers. 

If you use the show ip interface brief parser your data goes from being human-readable (like this):

LAB-1841-R1#show ip interface brief

Interface                  IP-Address      OK? Method Status                Protocol

FastEthernet0/0            203.0.113.1     YES NVRAM  up                    down

FastEthernet0/1            192.168.0.69    YES NVRAM  up                    up

Serial0/0/0                unassigned      YES NVRAM  administratively down down

Loopback0                  192.0.2.1       YES NVRAM  up                    up

To JSON-structured. Like this:

{'interface': {'FastEthernet0/0': {'ip_address': '203.0.113.1', 'interface_is_ok': 'YES', 'method': 'NVRAM', 'status': 'up', 'protocol': 'down'}, 'FastEthernet0/1': {'ip_address': '192.168.0.69', 'interface_is_ok': 'YES', 'method': 'NVRAM', 'status': 'up', 'protocol': 'up'}, 'Serial0/0/0': {'ip_address': 'unassigned', 'interface_is_ok': 'YES', 'method': 'NVRAM', 'status': 'administratively down', 'protocol': 'down'}, 'Loopback0': {'ip_address': '192.0.2.1', 'interface_is_ok': 'YES', 'method': 'NVRAM', 'status': 'up', 'protocol': 'up'}}} 

Despite appearances, this output works much better at being interpreted by scripts and used in programming languages to manipulate the behaviour of code or the network. This becomes a very powerful tool to use for event-driven decision to be made on the network based on environmental or outage-related issues. This is the principle behind software-defined networking and its ability to drive the re-routing of user traffic, determined by variables selected by the network administrator. 

This output is a dictionary and it's difficult to read. It's crammed onto the output and doesn't make much sense. You can use Code Beautify's website to tidy this up and make it more readable.

https://codebeautify.org/python-formatter-beautifier

Simply put your raw output in the box on the left and click on the 'format' button and the website will hierarchically structure the dictionary for you. This will give you a better view of the actual structure of the data you have parsed (which helps to drill into the dictionary for specific data, if this is what you're looking to achieve). 



Now our data looks like this:

{

    "interface": {

        "FastEthernet0/0": {

            "ip_address": "203.0.113.1",

            "interface_is_ok": "YES",

            "method": "NVRAM",

            "status": "up",

            "protocol": "down",

        },

        "FastEthernet0/1": {

            "ip_address": "192.168.0.69",

            "interface_is_ok": "YES",

            "method": "NVRAM",

            "status": "up",

            "protocol": "up",

        },

        "Serial0/0/0": {

            "ip_address": "unassigned",

            "interface_is_ok": "YES",

            "method": "NVRAM",

            "status": "administratively down",

            "protocol": "down",

        },

        "Loopback0": {

            "ip_address": "192.0.2.1",

            "interface_is_ok": "YES",

            "method": "NVRAM",

            "status": "up",

            "protocol": "up",

        },

    }

}

So much easier to read!

SC

Comments

Popular posts from this blog

pyATS - Reading from a Dictionary

Will FTTP ever replace Ethernet?