Networking: How Useful is pyATS in NetDevOps?

Written by verlainedevnet | Published 2023/11/09
Tech Story Tags: devops-tools | network-engineers | netdevops | network-engineering | devops-infrastructure | devops | pyats | python-virtual-environment

TLDRPyATS is a powerful tool that plays a crucial role in modern networking. Initially created at Cisco, this tool is used internally to run millions of tests per month. Although some core components of the tool are proprietary, it has now been opened up to the public. In this article, we will delve into what PyATS is and how it can benefit you in your networking endeavors. via the TL;DR App

While PyATS may have a difficult pronunciation😀, it is a Swiss knife in networking with its ability to achieve amazing things.

Despite NetDevOps simplifying the way we work with a plethora of tools, some companies still have a culture of fear where they don't want to touch what's working.

But with PyATS, there's no need to fear as it can automate and simplify network engineering tasks.

I had my first hands-on experience with PyATS in 2020 and have since become an evangelist of the tool. Thanks to John Capobianco, also known as the Bob Ross of automation, I continue to explore PyATS's capabilities and possibilities.

What is pyATS

PyATS has been made public in 2017 and stands for Python Automated Test System. It is a Python testing framework developed internally at Cisco, then made open source, and is now well-adopted worldwide for performing network tests. Cisco runs millions of tests monthly, including regression, sanity, and scale tests.

Additionally, the PyATS framework has a Python library called Genie, which contains all the network information and works on top of PyATS. Although some network engineers may have difficulty understanding the relationship between the two tools, it is crucial to note that where there is PyATS, Genie is present.

pyATS is the core framework and genie is the pyATS standard library. Genie is the SDK that contains everything we need for network test automation.

Key features

written in python,pyATS remains the most powerful testing framework for network engineers, some of the key features are :

  • Multi-vendor parsing: can parse unstructured data into structured data across multiple vendors, thanks to its plethora of parsers
  • Profiling and differential comparisons: pyATS is able to profile network state and perform differential comparisons.
  • Testing frameworks: pyATS provides frameworks to structure and provide reporting for tests
  • Scalability: One of the key advantages for developers in adopting a testing framework is the ability to start with small, simple, and linear test cases and then gradually scale up to larger and more complex test suites. This allows for a more controlled testing process, as developers are able to identify and address issues in a step-by-step manner.
  • Data-driven and reusable testing: pyATS is a testing framework that emphasizes reusability and data-driven testing, making it ideal for Agile development.

The layers

the pyATS framework is composed of various layers, and here are the following:

pyATS

The core framework plays a pivotal role in managing low-level mechanics, such as configuring device connections and executing device commands. Additionally, it serves as the foundation for an essential framework component known as the testbed, which outlines the topology to be tested. The testbed encompasses various objects, including devices, interfaces, and links, all represented as Python objects that can be interacted with. These objects offer multiple methods for carrying out actions and operations on them.

Have you explored all the awesome methods that the testbed offers? These nifty features make it super easy for us to perform various actions, such as:

device.connect()
device.ping("192.168.1.1")
device.execute("show version")

Here, the snippet does the following action:

  • device.connect() : the line establishes a connection to the network device, and the device object represennt a network device, and the connect() method is used to connect to it.
  • device.ping(“192.168.1.1”): The line sends a ping from the network device to the IP address “192.168.1.1”; the ping method is used for reachability of the specified IP address from the network device.
  • device.execute(“show version”): The line executes the command “show version” on the network device running Cisco IOS and shows the output; the execute method is used to run any command on the network device and retrieve its output.

Genie

Genie is a powerful tool that operates on the Pyats core layer, skillfully leveraging and abstracting the libraries and frameworks presented by the core. This is where the true magic takes place! With an impressive library and over 1500 parsers at its disposal, Genie expertly converts raw text into structured text.

Integrations

Atop the genie layer, we have the integrations layer that provides a wide range of libraries and plugins to deeply abstract the underlying pyATS/genie layers. This enables network engineers to fully utilize pyats at a higher level and take advantage of its capabilities.

PyATS core components

powerful and written in python,pyATS offers the following components :

  1. The foundational platform: it’s the original pyATS framework
  2. Library (Genie): it’s the platform vendor-agnostic library
  3. Web dashboard(Xpresso): it’s a beautiful web dashboard to manage test suites,testbeds, test results, and their insights.

Where to find pyATS?

With powerful and growing communities from internet and network junkies,pyATS can be found here :

Cisco Devnet offers the best documentation when it comes to pyATS, and it goes beyond Cisco!

to explore the documentation: PyATS and downloadable via PyPI

Quick setup

to install, there are some requirements follow and here are the following :

  • python 3.5+ must used or installed
  • choose a Linux distribution of your choice
  • if you are using Windows,WSL2 is a great choice

creating a Python virtual environment

#Create a virtual environment 
python3 -m venv pyATS_Test
#Activate the virtual environment 
source pyATS_Tests/bin/activate

installing pyATS

now, we are already inside our newly created virtual environment

pip3 install "pyats[full]==23.9"

pyATS 23.9 is the latest version to update to or install

validating the installation

How do we know if pyATS and Genie have been correctly installed? Here is the step:

$ pip3 freeze | grep -E "pyats=|genie="  
genie==23.9
pyats==23.9
Version

getting a CLI from a device

let’s see a use case where we can get simple CLI output (show IP interface brief) from an IOS XR device.

All the snippets you see were runned on Cisco IOS

Building a testbed

A great way to connect to a device is to create a testbed written in YAML, and the information will be used to connect to the device and send the requested commands.

# Step 0: list of devices
devices:
  iosxr1:
    # Step 1: OS and Type
    type: iosxr-Prod
    os: iosxr
    # Step 2: credentials
    credentials:
      default:
        username: admin
        password: Hacker@204k
    # Step 3: connection parameters
    connections:
      vty:
        protocol: ssh
        ip: test-mgmt.talesoftechnology.com
        port: 8080

What is the testbed?

  • Step 0: list of devices: This is where you define the devices in your network. In this case, there’s one device named iosxr1
  • Step 1: OS and Type: This specifies the operating system (os: iosxr) and the type (type: iosxr-Prod) of the device iosxr1.
  • Step 2: credentials: This is where you provide the login credentials for the device. The default credential set includes a username (username: admin) and a password (password: Hacker@204k).
  • Step 3: connection parameters: This section contains the details needed to connect to the device. The vty connection uses the SSH protocol (protocol: ssh) to connect to the IP address test-mgmt.talesoftechnology.com on port 8080.

The testbed is ready; let’s make our first pyATS script:

What are we going to achieve?

we are going to connect to the device and collect a CLI output of the show IP interface brief command.

from pyats.topology import loader

# Step 0: load the testbed
testbed = loader.load(f'./testbed.yaml')

# Step 1: testbed is a dictionary. Extract the device iosxr1
iosxr1 = testbed.devices["iosxr1"]

# Step 2: Connect to the device
iosxr1.connect(init_exec_commands=[], init_config_commands=[], log_stdout=False)

# Step 3: saving the `show ip interface brief` output in a variable
show_interface = iosxr1.execute('show ip interface brief')

# Step 4:printing the `show interface brief` output
print(show_interface)

# Step 5: disconnect from the device
iosxr1.disconnect()

What does the script do?

  • Step 0: It loads the testbed configuration from a file named testbed. yaml. This file contains details about the network devices, such as their IP addresses, credentials, and connection parameters.
  • Step 1: It extracts the device iosxr1 from the testbed. The testbed is essentially a dictionary where the keys are the device names and the values are objects representing the devices.
  • Step 2: It establishes a connection to the device iosxr1. The init_exec_commands=[] and init_config_commands=[] parameters are used to specify commands that should be run immediately after connecting to the device. In this case, no commands are specified. The log_stdout=False parameter is used to prevent the output of commands from being printed to stdout.
  • Step 3: It executes the command show IP interface brief on the device and saves the output in a variable named show_interface. This command displays a brief summary of the IP interfaces on the device.
  • Step 4: It prints the output of the show ip interface brief command to stdout. This allows you to see the status of the IP interfaces on your console.
  • Step 5: Finally, it disconnects from the device.

This script is useful for automating tasks on network devices, such as gathering information or configuring settings. Please note that you need to have pyATS installed and a valid testbed.yaml file for this script to work.

Executing the script

in our example, the file is named pyats_cli_show.py

from the bash terminal, we can execute

python pyats_cli_show.py 

The output!

Interface                      IP-Address      Status          Protocol Vrf-Name
Loopback99                     1.1.1.99         Up              Up      default 
Loopback100                    1.1.1.100        Up              Up      default 
Loopback999                    unassigned       Up              Up      default 
MgmtEth0/RP0/CPU0/0            192.168.1.1      Up              Up      default 
GigabitEthernet0/0/0/0         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/1         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/2         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/3         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/4         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/5         unassigned      Shutdown        Down     default 
GigabitEthernet0/0/0/6         unassigned      Shutdown        Down     default

Our short demo showed how to build a testbed and how to collect our first CLI output from the device.

Where does pyATS fit in netdevOps?

pyATS plays a crucial role in NetDevOps by providing a framework for network test automation; it fits well in the stateful test & validation domain by observing each part of the slice, presented by Cisco Devnet during a tech conference in 2019.

In summary, pyATS is an essential tool in the NetDevOps toolbox that helps streamline and standardize network operations and testing.

Use cases

pyATS is used worldwide! Here are some use cases where it shines :

  • Network Health Check Automation
  • Network Troubleshooting and Debugging
  • Network Test Automation

Conclusion

PyATS is a network testing framework that was initially developed at Cisco, where it was used to run millions of tests every month. In 2017, Cisco made pyATS open source, though some core components are still proprietary to Cisco systems.

Today, pyATS has become the de facto standard for network tests and boasts a growing community of contributors. It is considered the most powerful tool in the industry and is essential for network engineers who want to simplify their work with a Swiss knife.

As the field of netdevOps continues to evolve, it's crucial for network engineers to adopt pyATS and stay ahead of the curve.

If you enjoyed this short introduction to pyATS, consider subscribing to my contributor page to stay up-to-date with the latest developments in the field.


Written by verlainedevnet | I am a self-taught, polyglot and I write top-notch technical articles. I am between Network automation and pen-testing.
Published by HackerNoon on 2023/11/09