pyATS Connections

26.3 · active · verified Thu Apr 16

pyATS Connections is a sub-component of the pyATS end-to-end testing ecosystem, specializing in handling device connections to various network devices. It provides top-level abstractions and interfaces for creating connection classes, supporting CLI, REST, and NETCONF protocols. This library is crucial for enabling scripts to interact with network devices within the pyATS framework. The current version is 26.3, and it is actively maintained by Cisco Systems Inc. with frequent releases.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load a testbed definition from a YAML file, retrieve a device object, and attempt to establish a connection using `device.connect()`. It uses environment variables for credentials as a best practice. Note that the connection to a dummy IP (127.0.0.1) will fail in a real-world scenario without a reachable network device or mock device setup, but the code illustrates the API usage.

import os
from pyats.topology import loader

# Create a dummy testbed.yaml file for demonstration
with open('testbed.yaml', 'w') as f:
    f.write("""
devices:
  CSR1:
    type: router
    os: iosxe
    credentials:
      default:
        username: '%ENV{PYATS_USER}'
        password: '%ENV{PYATS_PASSWORD}'
    connections:
      cli:
        protocol: ssh
        ip: 127.0.0.1 # Use a dummy IP for local execution
        port: 22
""")

# Set dummy environment variables for quickstart (replace with actual or secure method)
os.environ['PYATS_USER'] = os.environ.get('PYATS_USER', 'devnetuser')
os.environ['PYATS_PASSWORD'] = os.environ.get('PYATS_PASSWORD', 'Cisco123!')

try:
    # Load the testbed from the YAML file
    testbed = loader.load('testbed.yaml')

    # Get a specific device from the testbed
    device = testbed.devices['CSR1']

    # Connect to the device (connection will fail with dummy IP, but demonstrates API)
    print(f"Attempting to connect to {device.name} at {device.connections['cli'].ip}...")
    device.connect(init_exec_params={'timeout': 5})
    print(f"Successfully connected to {device.name}.")
    
    # Execute a command (will only reach here if connect succeeds)
    output = device.execute('show version')
    print("Show Version output:\n", output)

except Exception as e:
    print(f"An error occurred: {e}")
    print("Note: Connection to 127.0.0.1 will likely fail unless a mock device is running or IP is changed.")
finally:
    # Clean up the dummy testbed file
    os.remove('testbed.yaml')

view raw JSON →