Unicon Connection Library Plugins

26.3 · active · verified Thu Apr 16

Unicon Plugins is a component of the Cisco pyATS framework, providing extensible platform support and services for the Unicon Connection Library. It enables unified command-line interface (CLI) connection experiences to various network devices, leveraging expect-like programming for multi-vendor support and seamless handling of CLI modes. Originally developed internally by Cisco, it was open-sourced in late 2017 through Cisco DevNet. The current version is 26.3, with releases typically aligning with the pyATS framework's frequent updates, often on a monthly or bi-monthly cadence.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a basic CLI connection to a network device using `unicon.Connection`, leveraging the plugins to handle device-specific interactions. It uses environment variables for credentials, which is a common practice in automation. For a real environment, device details are typically managed via a pyATS Testbed YAML file.

import os
from unicon import Connection

# Example: Connecting to an IOS XE device
# In a real scenario, device details and credentials would typically come from a pyATS testbed YAML file.
# For a quick start, we'll simulate a direct connection.

hostname = os.environ.get('DEVICE_HOSTNAME', 'your_device_ip_or_hostname')
username = os.environ.get('DEVICE_USERNAME', 'admin')
password = os.environ.get('DEVICE_PASSWORD', 'Cisco123')
enable_password = os.environ.get('DEVICE_ENABLE_PASSWORD', 'Cisco123')

# Instantiate a connection object for an IOSXE device
# The 'os' argument directs Unicon to load the appropriate plugin.
# Other parameters like 'platform' and 'model' can further refine plugin selection.
try:
    print(f"Attempting to connect to {hostname} (OS: iosxe)...")
    connection = Connection(
        hostname=hostname,
        os='iosxe',
        start=['ssh {username}'], # Specifies how to start the connection
        log_file='unicon_connection.log', # Optional: logs interaction
        init_exec_commands=['terminal length 0', 'terminal width 0'], # Initial commands to run
        custom_callbacks={
            'password': lambda: password,
            'enable_password': lambda: enable_password
        }
    )
    
    connection.connect()
    print(f"Successfully connected to {hostname}")
    
    # Execute a command
    output = connection.execute('show version')
    print("\n--- show version output ---\n")
    print(output[:500]) # Print first 500 characters of output
    print("\n---------------------------")
    
    # Configure the device (example - requires 'configure' service for the plugin)
    # config_commands = ['interface Loopback100', 'description Test Loopback']
    # print(f"\nAttempting to configure: {config_commands}")
    # connection.configure(config_commands)
    # print("Configuration applied.")
    
except Exception as e:
    print(f"Connection failed: {e}")
finally:
    if 'connection' in locals() and connection.is_connected():
        connection.disconnect()
        print(f"Disconnected from {hostname}")

view raw JSON →