Devnet
YANG
Netconf
The ncclient is a widely-used open-source NETCONF client for Python, designed to provide an intuitive API that effectively translates the XML-encoded nature of NETCONF to Python objects and constructs. It handles client-to-server connection, authentication, capability exchange, and connection management. Ncclient supports all operations and capabilities defined in RFC 4741. Writing network management scripts becomes more straightforward with ncclient as it reduces the need for XML usage by employing Python objects.
 
To establish a connection to a device, send XML documents, and receive responses, only a few Python statements are required. Here's an example using the Python interpreter:
 
```python
from ncclient import manager
import lxml.etree as ET
 
# Establish connection
device = manager.connect(
    host="csr1kv1",
    port=830,
    username="cisco",
    password="cisco",
    timeout=90,
    hostkey_verify=False
)

While defaults are often used, critical parameters like host, port, username, password, and device_params should be updated as per device specifications. To limit response output, a filter needs to be defined. This filter specifies the desired content and resembles a RESTCONF URL.

Here's an example filter:

get_filter = """
<filter xmlns="urn:ietf:params:xml:ns:netconf:base:1.0">
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name/>
      <description/>
      <type/>
      <enabled/>
    </interface>
  </interfaces>
</filter>
"""

To retrieve configuration, use the get method and apply the defined filter:

nc_get_reply = device.get(('subtree', get_filter))
print(ET.tostring(nc_get_reply.data_ele, pretty_print=True))

To modify configuration, create a configuration data variable with appropriate changes and use the edit_config method:

edit_gig1_conf = """
<config>
  <interfaces xmlns="urn:ietf:params:xml:ns:yang:ietf-interfaces">
    <interface>
      <name>GigabitEthernet1</name>
      <description>Description was changed via NCClient</description>
    </interface>
  </interfaces>
</config>
"""

Verify the changes by retrieving the configuration again.

This process simplifies network management tasks, akin to the capabilities of the show and configure commands in traditional CLI interfaces.

```markdown
As a continuation, here's the process to retrieve and verify the configuration changes:

```python
# Retrieve configuration from the running datastore
response = device.get_config('running', get_conf)
print(ET.tostring(response.data_ele, pretty_print=True))

The output should reflect the updated configuration for GigabitEthernet1. This methodology streamlines configuration management tasks, similar to traditional CLI interactions.

For instance, if we were to check the description of GigabitEthernet1 interface after the change:

csr1kv1# show run interface GigabitEthernet1
Building configuration...
 
Current configuration : 157 bytes
!
interface GigabitEthernet1
 description Description was changed via NCClient
 no ip address
 shutdown
 negotiation auto
 no mop enabled
 no mop sysid
end

This demonstrates how ncclient facilitates configuration adjustments, making network management more efficient and Python-centric.


This addition completes the explanation of how ncclient can be used for configuration retrieval, modification, and verification, aligning with traditional CLI procedures.