Devnet
pyATS
5.connections

Connections in pyATS

This guide explains how to establish connections to network devices using pyATS.

Overview

Connections within pyATS define various methods for obtaining management access to devices described in a testbed. Each connection is represented as a class object following object-oriented principles, irrespective of protocol (e.g., Telnet, SSH) or technology (e.g., CLI, REST).

Connections in Testbed File

A testbed file, typically in YAML format, outlines devices under testing and their interconnections. The connections section within this file specifies management access for each device, with nested dictionaries containing key-value pairs. Each connection requires a unique user-defined name, accommodating different access methods and IP addresses.

Available Keys:

  • class: Default connection implementation class.
  • alias: Alias name for pyATS API methods.
  • host: Device hostname.
  • ip: Access management IP address.
  • port: TCP/UDP port for access.
  • credentials: Specific to the connection, containing nested key-value pairs.

Credentials are essential for each connection, configurable globally or per-device basis within the testbed file.

Connection Package

The pyATS connections package facilitates connection implementation and execution based on testbed definitions. It comprises three essential classes:

  • ConnectionManager: Manages resource allocation, control, and conflict resolution.
  • BaseConnection: Provides the foundation for device connection implementations.
  • ConnectionPool: Supports creating connection pools for multiprocessing environments.

ConnectionManager Attributes and Methods

The ConnectionManager class, unique to each device object, controls connection procedures and functions, offering various attributes and methods for testscript usage.

NameDescription
deviceParent device object belonging to the manager.
destroy()Destroys or deletes a connection.
connectionsDictionary storing connection instances.
is_connected()Checks if a connection is alive.
instantiate()Creates a connection or pool object.
disconnect_all()Disconnects all existing connections.
connect()Establishes connection and creates connection object.
destroy_all()Destroys or deletes all connections.
disconnect()Disconnects an existing connection.

For detailed information, refer to the official documentation here (opens in a new tab).

Connection-Supported Operating Systems and Platforms

pyATS supports a wide range of operating systems and platforms, facilitating connectivity and operability with diverse devices. Vendor-agnostic in nature, it embraces platforms like IOS XE, IOS XR, NX-OS, IOS, FXOS, SD-WAN, among others. For a comprehensive list, consult the Unicon Plugins documentation (opens in a new tab).

Managing Device Connections in pyATS Shell

Device connections can be managed using the pyATS shell or Python code. The process involves loading the testbed YAML file, accessing specific devices, establishing connections, executing commands, and handling session termination.

Example:

$ pyats shell 
from genie.testbed import load
tb = load('my_testbed_file.yaml')
csr1 = tb.devices['CSR1']
csr1.connect()
uptime = csr1.execute('show version | i uptime')
p1 = csr1.parse('show inventory')
print(uptime)
csr01.disconnect()

These commands facilitate interaction with devices and execution of CLI commands, maintaining connectivity and proper session management.