07-04-2024 08:59 PM
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
Solved! Go to Solution.
07-05-2024 03:03 AM
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.
07-05-2024 03:03 AM
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.
07-06-2024 06:14 PM
Thanks much @bigevilbeard for the update.
07-07-2024 04:50 AM
No problem @rys
07-10-2024 03:41 AM
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
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide