DEV Community

Bilal
Bilal

Posted on

pyATS, the Cisco python automation tool

Automation is a core element in the management of network devices.
In this post, I tried to set up the pyATS toolset by Cisco. It's a library that can be used in full python code and has a CLI tool add-on. It's vendor agnostic, and you can run your tests or operations tasks.

The installation

pyATS needs to be installed on Linux based system, as I'm using a Windows setup, I installed it on WSL.
I installed pyATS with the Python package manager, pip :

pip install "pyats[full]"
Enter fullscreen mode Exit fullscreen mode

Once installed, the libraries are available to import in your Python code along with the CLI tool.

Write the python code

Your devices have to be loaded in a testbed instance.
The said devices are defined in a YAML file.
Once your target device is selected, you can extract and parse the result of a command.
The pyATS ecosystem has built-in parser, for commands like show inventory, but you can write your own parser for specific use-case.

from genie.testbed import load

testbed = load('my_testbed.yaml')
dev = testbed.devices['nx-osv-1']
dev.connect()
p1 = dev.parse('show inventory')

print('Slot 1 serial number:' + p1['name']['Slot 1']['serial_number'])
Enter fullscreen mode Exit fullscreen mode

My devices to TestBed

Devices information have to be stored in a yaml file, my_testbed.yaml in the above example. It contains device information like ip addresses, OS, hostname, login, password, and so on.

devices:
  R1:
    connections:
      cli:
        ip: 10.10.10.11
        protocol: ssh
    credentials:
      default:
        password: password
        username: admin
      enable:
        password: password
    os: iosxe
    platform: asr1k
    type: iosxe
Enter fullscreen mode Exit fullscreen mode

To avoid any typo, a csv(or xls) file can be used to store the devices data.

Devices information in CSV file

Once all values completed, you can then convert the csv file to YAML format using the pyats cli:

(pyATS) ➜  pyats create testbed file --path ./my_devices.csv --output ./my_testbed.yaml
Enter fullscreen mode Exit fullscreen mode

Top comments (0)