using pyats to automate network test and configuration
pyATS is intended for test script developers and network engineers who want to get started with pyATS and the pyATS Library (previously "Genie").
pyATS currently supports Python v3.7.x, v3.8.x, v3.9.x and v3.10.x, and is tested to work on the following platforms:
Installation
pip install "pyats[full]"
Getting started with testbed.yml
To generate a testbed file for PyATS connectivity, you can convert an existing Ansible hosts file into a testbed file, or you can create a custom testbed file by leveraging an API to pull data from a CMDB or an external database.
Using Ansible - run.yml
---- name: Create Pyats Testbed From Ansible Inventory hosts: "{{ _devices }}" gather_facts: no vars: _devices: - cisco tasks: - name: Flatten Group Name To Device List. set_fact: _testbed: '{{ _devices | map("extract", groups) | flatten | list }}' - name: Template Ansible Inventory Devices To Testbed.yml template: src: testbed.j2 dest: testbed.yml
from pyats.topology import loaderfrom genie.libs.parser.utils import get_parser_excludedef main(): # Load the testbed testbed = loader.load('testbed.yaml') # Connect to the router device = testbed.devices['r1'] device.connect(log_stdout=False) # Execute the command to get interface information output = device.parse('show interfaces') # Assuming the internet link is on interface GigabitEthernet0/0 interface = output['GigabitEthernet0/0'] # Print the bandwidth print(f"Interface: {interface['interface']}") print(f"Bandwidth: {interface['bandwidth']} Kbps") # Disconnect from the device device.disconnect()if __name__ == '__main__': main()
Shell mode
If you want to use Python, you can use pyats shell to load the testbed API and create your testbed and device objects. Then, tell the system to connect to each device and to learn the specified features. In this example, the system stores the output as a Python dictionary in the variable learnt and displays the output: