NETCONF Client for Python
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
- breaking Version 0.7.0 introduced `ssh-python` as an optional SSH transport, which means the `manager.connect` call is NOT 100% backwards compatible with all possible parameters that may have been passed when only `Paramiko` was used. Code relying on specific `Paramiko` nuances might break.
- breaking Support for Python 2 was officially removed in version 0.6.17. Users on Python 2.x will encounter errors and should upgrade their Python environment.
- gotcha By default, `ncclient` uses Paramiko for its SSH transport. As of v0.7.0, an alternative `ssh-python` transport can be used by installing `ncclient[libssh]`. This choice can affect behavior and dependency requirements, as `ssh-python` typically depends on the system's `libssh` library.
- gotcha Using `hostkey_verify=False` in `manager.connect` disables SSH host key checking, which is insecure and not recommended for production environments. This is often seen in examples for simplicity.
- gotcha The `async_mode` attribute, which was previously a direct attribute of the `Manager` instance for asynchronous RPC requests, had changes in its implementation/usage around version 0.6.0 for compatibility with Python 3.7+.
- gotcha For vendor-specific NETCONF operations and capabilities, `ncclient` relies on device handlers specified via `device_params={'name': '<vendor_alias>'}`. Failing to set this correctly for a specific device type (e.g., 'junos', 'nexus') might lead to unexpected behavior or missing functionality.
Install
-
pip install ncclient -
pip install ncclient[libssh]
Imports
- Manager
from ncclient import manager
- connect
from ncclient import manager
Quickstart
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()