Python NETCONF Client: The Ultimate Tool You Didn’t Know You Needed

Python netconf client is a Python library that helps you in establishing a connection with the NETCONF devices. NETCONF stands for Network Configuration. It makes use of Python totally. With this library, you can develop in-depth knowledge of the configuration of network devices. This happens even if your device is in a remote area, i.e., it’s distant from you.

Supported device handlers

A Supported device handler refers to a library in Python that helps to interact with network devices. Such a library follows Python’s OOPS feature of abstraction. It has easy scripts for the users. The complex code is hidden. Python offers several supported device handlers, some of which are:

  • ncclient
  • NAPALM
  • Netmiko
  • zigpy

NAPALM is one such library that supports a plethora of devices. However, if you need interaction with NETCONF, ncclient is the best.

Benefits of Python NETCONF clients

We can say that the NETCONF clients work on the given three principles:

  • Flexibility
  • Community support
  • Automation

This is because Python is one of the easiest languages to interpret the process of management of your networking devices. In addition to this, it boosts efficiency as the configuration can be established in a short time period.  Python NETCONF is used by many people, so there’s a strong community that can be of help to you if you are stuck.

Usage of NETCONF clients

NETCONF clients are used for a variety of reasons, some of which are:

  1. Check the status of the network device.
  2. Set and alter the configuration of the network device.
  3. Obtain configuration of the network device.

All these methods can be used to troubleshoot NETCONF issues, add new devices and configure them accordingly, or change the setup of existing devices.

Prerequisites for Python NETCONF

Python NETCONF involves a few prerequisites too. Two of the commonly required prerequisites are:

  • Installation of Python
  • Python NETCONF client library like netconf_client or ncclient installation.

Python NETCONF clients

There are two commonly used netconf clients in Python. These are:

  • netconf_client
#for installation
pip install netconf_client
  • ncclient
#for installation
pip install netconf_client

Syntax for installation

Python NETCONF clients can be installed using the same library. A command named pip has to be used for the same.

pip install netconf_client

Connection to server

In order to start using NETCONF on the local host, you can use SSH. Enter the username root and password as password. This code prints the configuration

import netconf_client

# use SSH for server connection
session = netconf_client.connect(
    host="localhost",
    port=22,
    username="root",
    password="password"
)

# obtain the device's configuration
config = session.get_config()

# Print the configuration
print(config)

Editing the configuration

After successfully connecting your device to the server, you can use the NETCONF client to share messages. Thus, now you can send and receive messages too. The following piece of code lets you edit the configuration.

import netconf_client

# use SSH for server connection
session = netconf_client.connect(
    host="localhost",
    port=22,
    username="root",
    password="password"
)

# make changes to  the configuration
config = session.edit_config(
    "<config>"
    "  <system>"
    "    <name>New Device Name</name>"
    "  </system>"
    "</config>"
)

Fixing Python netconf client issues

There can be issues with the networked devices. In order to fix them, check out these methods:

  •  Go through the Documentation for the Python NETCONF client library.
  • Take community help
  • Back up your network devices before configuration.
  • Create a test environment to check the configuration of your device rather than using it directly.
  • Keep monitoring the status of all devices you have connected the library to.

Usage of the ncclient module

This code explains how ncclient works in Python.

# Import the ncclient library
import ncclient

# Connect to a Juniper JunOS device
device = ncclient.connect('192.168.1.2', 'juniper', 'juniper', hostkey_verify=False)

# Obtain the configuration of the device
config = device.get_config()

# Print the configuration 
print(config)

# Close the established connection to the device
device.close()

ncclient.capabilities.schemes

It suggests different ways in which it can transfer the data. It has a parameter as scheme a query string (i.e., capability URL), which returns the list of the device-accepted schemes as the output.

import ncclient
# Connect to a NETCONF device
device = ncclient.connect('192.168.1.1', 'cisco', 'cisco', hostkey_verify=False)

# Get the supported schemes for the :url capability
schemes = ncclient.capabilities.schemes('urn:ietf:params:netconf:capability:url:1.0?scheme=ftp')

# Print the supported schemes
print(schemes)

# Close the connection to the device
device.close()

ncclient.capabilities.Capabilities

It is similar to the previous one, but it suggests whether a netconf client supports a particular capability or not.

import ncclient

# Connect to a NETCONF device
device = ncclient.connect('192.168.1.1', 'cisco', 'cisco', hostkey_verify=False)

# Get the capabilities of the device
capabilities = device.capabilities

# Check if the device supports the :url capability
if 'urn:ietf:params:netconf:capability:url:1.0' in capabilities:
    print('The device supports the :url capability.')

# Close the connection to the device
device.close()

So, we get the output as follows: The device supports the: URL capability.

Is the netconf_client package maintained well?

netconf_client is definitely maintained well. It has a strong community. The contributors update the version after every six months. It supports over 1,500 weekly downloads too. Python users are really active in the community. So, users handle cross queries well.

FAQs

Which Python NETCONF client should one use?

It depends on the user’s needs. ncclient is more advanced and more complex than netconf. Netconf is simpler and suggested for beginners.

Which NETCONF client supports TLS sessions?

netconf_client supports TLS sessions.

Conclusion

In this blog, you will have to know what a NETCONF client is and its implementation. You will get to know how this library can be installed. Moreover, it also suggests the varied uses of netconf clients.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments