04-03-2024 06:40 AM
Hello,
I'm building a small python script using pyATS library and I would like to verify if the configuration commands are valid (command syntax and mistype) before sending it to the device, so I don't send half the configuration to the device. Is there a dry run built-in function i pyATS for that?
Thank you
04-03-2024 07:04 AM
I dont think so... , you can use genie's parse_candidate function to test the syntax of your configuration commands. In this example below, the config_commands is a list of configuration commands that are to be tested. Then the parse_candidate function is used to parse the candidate configuration commands and check for syntax errors. If there is a syntax error in any of your commands/syntax, an exception is raised, and you can catch it and print an error message. Something like this (use with caution!)
from genie.testbed import load
from genie.parse import ParseOutput
testbed = load('testbed.yaml')
device = testbed.devices['device_name']
device.connect()
config_commands = ['interface ethernet 1/1', 'shutdown']
output = ParseOutput(device=device, cmd='configure terminal')
for command in config_commands:
output.append(command)
try:
output.parse()
except Exception as e:
print(f'Error parsing command "{command}": {e}"')
device.disconnect()
See this blog post by Dan https://devnetdan.com/2021/07/12/pyats-and-genie-part-3/ this will also prove useful https://learningnetwork.cisco.com/s/blogs/a0D6e00000sR7Q6EAK/testdriven-automation-with-pyats
Hope this helps.
04-03-2024 11:52 AM
I couldn't test the code example because I'm getting an error when importing the ParseOutput function, it's complaining the 'genie.parse' module doesn't exist.
The links are very useful and I'm planning to work in some baseline/tests as well.
To set a little more the scene, the script I'm working at is to send adhoc commands (it could be to gather information or configure) a list of devices, the gathering part is working fine and I'm handling the invalid command and syntax error with exceptions but on the configuration part I didn't manage to validate the commands before pushing it to the device. Since my idea is for this script be as generic as possible one should be able to configure anything in the device if have provided the correct command syntax.
04-04-2024 01:47 AM
Sorry, i think then parseoutput func is part of the genie.libs.parser.utils module, not genie.parse? Try
from genie.testbed import load
from genie.libs.parser.utils import ParseOutput
testbed = load('testbed.yaml')
device = testbed.devices['device_name']
device.connect()
config_commands = ['interface ethernet 1/1', 'shutdown']
output = ParseOutput(device=device, cmd='configure terminal')
for command in config_commands:
output.append(command)
try:
output.parse()
except Exception as e:
print(f'Error parsing command "{command}": {e}"')
device.disconnect()
Thanks for the added info, for the script to be as generic as possible, you could use Jinja2 templates to generate the configuration commands based on your input and this way, you can separate the configuration logic from the script, making it more modular and reusable. You can then use the parse_candidate function to validate the generated configuration before sending it to the device itself. Some sample here on J2 with pyats - https://www.automateyournetwork.ca/pyats/my-first-pure-python-network-automation-with-pyats-genie/
Hope this helps.
04-05-2024 03:36 AM
I appreciate the input and I'll change my approach
Thanks for the help
04-05-2024 04:59 AM
Happy to help @cabral-edson
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