YANG Connector

26.3 · active · verified Thu Apr 16

YANG Connector is a Python package providing tools for testing YANG models via NETCONF and RESTCONF protocols. Primarily, it offers a NETCONF client by wrapping the `ncclient` library and integrates seamlessly with the pyATS framework. The library is actively maintained, with the current version being 26.3, and supports NETCONF v1.0 and v1.1.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to connect to a network device using `yang.connector.Netconf` via the pyATS testbed loader. It then retrieves a filtered configuration (e.g., interfaces) and lists the device's NETCONF capabilities. Credentials should be provided via environment variables for security.

import os
from pyats.topology import loader

# Define device connection details using environment variables for security
DEVICE_IP = os.environ.get('YANG_CONNECTOR_DEVICE_IP', 'your_device_ip')
DEVICE_PORT = int(os.environ.get('YANG_CONNECTOR_DEVICE_PORT', 830)) # Default NETCONF port
USERNAME = os.environ.get('YANG_CONNECTOR_USERNAME', 'admin')
PASSWORD = os.environ.get('YANG_CONNECTOR_PASSWORD', 'admin')

# A minimal pyATS Testbed YAML string for demonstration
testbed_yaml = f'''
devices:
  my_device:
    os: iosxe
    connections:
      netconf:
        class: yang.connector.Netconf
        ip: {DEVICE_IP}
        port: {DEVICE_PORT}
        protocol: netconf
        credentials:
          default:
            username: {USERNAME}
            password: {PASSWORD}
'''

try:
    # Load the testbed from the YAML string
    testbed = loader.load(testbed_yaml)
    device = testbed.devices['my_device']

    # Establish connection
    device.connect(via='netconf', init_exec_alias=False)
    print(f"Successfully connected to {device.name} via NETCONF.")

    # Example: Get configuration using get_config()
    # For a full configuration, an empty filter can be used
    # For specific data, provide an XML filter string
    filter_xml = '<filter type="subtree"><interfaces/></filter>'
    config_data = device.netconf.get_config(source='running', filter=filter_xml)
    print("\nRetrieved configuration (filtered for interfaces):\n")
    print(config_data.data_xml)

    # Example: Get capabilities
    print("\nServer Capabilities:")
    for cap in device.netconf.server_capabilities:
        print(f"- {cap}")

except Exception as e:
    print(f"An error occurred: {e}")
finally:
    # Disconnect from the device
    if 'device' in locals() and device.is_connected('netconf'):
        device.disconnect(via='netconf')
        print(f"Disconnected from {device.name}.")

view raw JSON →