NETCONF Client for Python

0.7.1 · active · verified Mon Apr 13

ncclient is a Python library that facilitates client-side scripting and application development around the NETCONF protocol. It aims to offer an intuitive API that maps XML-encoded NETCONF to Python constructs, making network-management scripts easier. The library is currently at version 0.7.1 and is actively maintained with releases happening periodically based on feature additions and bug fixes.

Warnings

Install

Imports

Quickstart

This quickstart connects to a NETCONF-enabled device, retrieves its capabilities, and then fetches a filtered portion of the running configuration. It uses environment variables for connection details for security and flexibility. The `hostkey_verify=False` is used here for simplicity, but should be replaced with proper host key verification in production environments.

import os
from ncclient import manager
from ncclient.transport import SSHError

HOST = os.environ.get('NETCONF_HOST', 'your_netconf_device_ip')
PORT = int(os.environ.get('NETCONF_PORT', 830))
USER = os.environ.get('NETCONF_USER', 'admin')
PASSWORD = os.environ.get('NETCONF_PASSWORD', 'password')

def get_device_capabilities():
    try:
        with manager.connect(host=HOST, port=PORT, 
                             username=USER, password=PASSWORD, 
                             hostkey_verify=False, 
                             device_params={'name': 'default'}) as m:
            print(f"Connected to {HOST} (Session ID: {m.session_id})")
            print("\n--- Server Capabilities ---")
            for capability in m.server_capabilities:
                print(f"  - {capability}")
            print("\n--- Get Running Configuration (filtered) ---")
            config_filter = '''
                <filter type="subtree">
                    <system xmlns="urn:ietf:params:xml:ns:yang:ietf-system"/>
                </filter>
            '''
            result = m.get_config(source='running', filter=config_filter).data_xml
            print(result)
            m.close_session()
    except SSHError as e:
        print(f"SSH Connection Error: {e}")
    except Exception as e:
        print(f"An unexpected error occurred: {e}")

if __name__ == '__main__':
    get_device_capabilities()

view raw JSON →