Unicon Connection Library

26.3 · active · verified Thu Apr 16

Unicon is a Python package that provides a unified connection experience to network devices via typical command-line management interfaces (CLI). It serves as the standard CLI connection implementation within the Cisco pyATS framework, offering direct and proxied connections, expect-like programming, and multi-vendor support through an agnostic API. Initially developed internally by Cisco, it was released publicly in late 2017 through Cisco DevNet. [1, 2, 12] The current version is 26.3, released on April 2, 2026, and the project maintains an active release cadence, often aligned with the broader pyATS/Genie framework releases. [2, 23]

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to establish a basic CLI connection to a network device using Unicon, execute a command, and then disconnect. It uses environment variables for sensitive credentials for security and easy testing. [12]

import os
from unicon.core.connection import Connection

# Replace with your device details and credentials
device_settings = {
    'hostname': os.environ.get('DEVICE_HOSTNAME', '10.0.0.1'),
    'os': os.environ.get('DEVICE_OS', 'ios'), # e.g., ios, nxos, iosxe
    'type': os.environ.get('DEVICE_TYPE', 'router'),
    'username': os.environ.get('DEVICE_USERNAME', 'admin'),
    'password': os.environ.get('DEVICE_PASSWORD', 'Cisco123'),
    'enable_password': os.environ.get('DEVICE_ENABLE_PASSWORD', 'Cisco123'),
}

try:
    # Create a connection instance
    conn = Connection(hostname=device_settings['hostname'],
                      os=device_settings['os'],
                      type=device_settings['type'],
                      start_state='exec') # Specify the initial state to reach

    # Provide credentials (Unicon can also retrieve from testbed YAML)
    conn.settings.CLI_USERNAME = device_settings['username']
    conn.settings.CLI_PASSWORD = device_settings['password']
    conn.settings.CLI_ENABLE_PASSWORD = device_settings['enable_password']

    # Connect to the device
    print(f"Connecting to {device_settings['hostname']}...")
    conn.connect()
    print("Connection successful.")

    # Execute a command
    output = conn.execute('show version')
    print("\n--- show version output ---")
    print(output)

    # Disconnect from the device
    conn.disconnect()
    print("Disconnected.")

except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →