cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
543
Views
1
Helpful
4
Replies

Parsing using pyATS

rys
Level 1
Level 1

Hi team,
I am looking to prepare a script for config translator for one of the migration projects from Cisco NX-OS and considering many of the parsing tools which can help here.

Will the pyATS can parse the "show run" to get the output in json format similar to what we can do with TTP ?

I saw comments on capturing and diff the show run with pyATS, but not specific to parsing the "show run" output and build a new config using the json or yaml output.

Thanks

rys

1 Accepted Solution

Accepted Solutions

Hey @rys yes you can do this with pyATS, you parse the "show run" output and provide it in JSON format. You can use the parse method to do this. For example (update as you need)

# Connect to the device
testbed.devices['nx-osv-1'].connect()

# Parse the "show run" output
output = testbed.devices['nx-osv-1'].parse('show run')

# Convert the output to JSON
json_output = output.to_json()

print(json_output)

This is documented here too https://developer.cisco.com/docs/pyats/parsing-device-output/

You can also use the pyats parse command from the CLI too, for example

pyats parse "show run" --testbed-file testbed.yaml

You can then use this JSON output to build a new config using your preferred method with both these examples above. You can then use the output to build the new config, for example

json_output = '''
{
    "nxos:config": {
        "nxos:cmds": [
            "feature telnet",
            "feature ssh",
            "vlan 100",
            "vlan 200",
            "interface Ethernet1/1",
            "  ip address 10.1.1.1/24",
            "interface Ethernet1/2",
            "  ip address 10.1.2.1/24"
        ]
    }
}
'''

I would maybe use a templating engine like Jinja2 to generate the new config from the JSON output with this, the perfect advantage of using a templating engine is that you can define a lot more complex template with conditional logic, loops, and variables, making it easier to generate customized configs later. Its a a little more a of a build at the start, but the long term this pays off imo.

Hope this helps.

 

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

View solution in original post

4 Replies 4

Hey @rys yes you can do this with pyATS, you parse the "show run" output and provide it in JSON format. You can use the parse method to do this. For example (update as you need)

# Connect to the device
testbed.devices['nx-osv-1'].connect()

# Parse the "show run" output
output = testbed.devices['nx-osv-1'].parse('show run')

# Convert the output to JSON
json_output = output.to_json()

print(json_output)

This is documented here too https://developer.cisco.com/docs/pyats/parsing-device-output/

You can also use the pyats parse command from the CLI too, for example

pyats parse "show run" --testbed-file testbed.yaml

You can then use this JSON output to build a new config using your preferred method with both these examples above. You can then use the output to build the new config, for example

json_output = '''
{
    "nxos:config": {
        "nxos:cmds": [
            "feature telnet",
            "feature ssh",
            "vlan 100",
            "vlan 200",
            "interface Ethernet1/1",
            "  ip address 10.1.1.1/24",
            "interface Ethernet1/2",
            "  ip address 10.1.2.1/24"
        ]
    }
}
'''

I would maybe use a templating engine like Jinja2 to generate the new config from the JSON output with this, the perfect advantage of using a templating engine is that you can define a lot more complex template with conditional logic, loops, and variables, making it easier to generate customized configs later. Its a a little more a of a build at the start, but the long term this pays off imo.

Hope this helps.

 

 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

rys
Level 1
Level 1

Thanks much @bigevilbeard for the update.

No problem @rys 

Please mark this as helpful or solution accepted to help others
Connect with me https://bigevilbeard.github.io

rys
Level 1
Level 1

Forgot to ask this in my earlier thread.
All I can see is pyATS required to SSH to device to run the commands and to parse the output.

Should this possible to parse the show commands without SSH to the device ?
Many times, the customers are providing only 'show run' commands.

Thanks in advance