NETCONF 101: Introduction to NETCONF

In this Learning Lab, you will learn the basics of the NETCONF protocol and how to establish a session to a device presenting a NETCONF API using Python.

Objective

Completion Time: 30 minutes

  • Understand the benefits of NETCONF
  • Understand the basics of the NETCONF protocol
  • Understand the basics of consuming NETCONF APIs
  • Use Python to connect to a device's NETCONF subsystem

Prerequisites

You will want to make sure you have gone through the Coding 101 lab if you are unfamiliar with retrieving results from a RESTful service. Although this lab will not use a REST API, there are similar concepts that will be useful from this previous lab.

In this lab, we are going to use Putty or an equivalent terminal emulator and a simple Python script to connect to a device's NETCONF subsystem.

Also, this lab includes steps in the How to Setup Your Computer section to setup your development environment if needed.

Background

  • If you are unfamiliar with Python, make sure you complete the Coding 10X and Coding 20X learning lab exercises.

  • This lab shall shall use Virtual Nexus Switches as a programmable devices running in DevNet's VIRL Always On VIRL/NX-API Sandbox

Access to a Device that Supports a NETCONF API

  • If you are working from your own machine, use the DevNet Sandbox Always On VIRL/NX-API Sandbox
    • This is a small Nexus topology to which we can send NETCONF API calls

Python

  • To run the code samples, you need to have Python2 installed on your machine.

Virtual Environments

  • This lab uses a venv to run ncclient is 0.5 and avoid issues with existing Python environments.
  • Reference the section How to Set up Your Computer above to learn how to complete this step.

Development Libraries

  • If you are running on a Ubuntu based systems, we'll need the development version of Python, libxml2, and libxslt as well as a few others.
  • Reference the section How to Set up Your Computer above to learn how to complete this step.

NCClient Library

  • These codes samples use the NCClient to simplify making NETCONF API calls.
  • Note that currently, the most recent version of ncclient is 0.4.7. The latest development release in GitHub (0.5.0 at the time this lab was written) has support for Python3 as well as some useful classes that future learning labs shall leverage. The steps below show how to install this version.
  • Reference the section How to Set up Your Computer above to learn how to install the NETCONF Library on your own machine.

Clone Git Repo

  • Clone the NETCONF example code
    • This GitHub repository provides sample code that you can run for this learning lab
  • If you are working from your own computer, clone the git repository.
  • Reference the section How to Set up Your Computer above to see the steps required.

Step 1. Understanding the benefits of NETCONF

NETCONF provides a consistent, programmatic interface across devices from different vendors and different operating systems. NETCONF also helps simplify network and device automation. When it comes to automating networks and network devices, there are a number of challenges such as those listed below.

  1. Inconsistencies across platforms
    • Networking devices from different vendors or running different operating systems often have different programmatic interfaces
  2. Unstructured data
    • Although the command-line interface (CLI) is the most well-known interface into network devices, the data from CLI is unstructured and cumbersome to operate programmatically

To see an example of these issues in action, take a look at the following Python code example which parses the hostname of a Nexus9KV.

import re
import sys


def main():
    """
    Open a file called sandbox-nexus9kv-config.txt.
    Print each line that matches a regular expression for a hostname route.
    """
    HOSTNAME = ''
    NXOS_HOSTNAME_REGEX = '^hostname (.*)
NXOS_DOMAIN_REGEX = '^ip domain-name (.*)
with open('sandbox-nexus9kv-config.txt', 'r') as nexus_config: for line in nexus_config: if re.match(NXOS_HOSTNAME_REGEX, line): HOSTNAME = re.search(NXOS_HOSTNAME_REGEX, line).group(1) + '.' elif re.match(NXOS_DOMAIN_REGEX, line): HOSTNAME += re.search(NXOS_DOMAIN_REGEX, line).group(1) print(HOSTNAME) if __name__ == '__main__': sys.exit(main())

The file sandbox-nexus9kv-config.txt contains CLI configuration for a NEXUS9KV. Running the Python script above should the hostname of the device.

After running the script, we can see that the Python script parses each line of the configuration. If a particular line matches the IOS command ip domain-name or hostname , the line will parse the from the syntax to create the hostname.

Here is an example of the script in action.

ubuntu$ python screen_scraping.py
n9kvswitchfcs.cisco.com

The example above illustrates some of the challenges in parsing legacy CLI to automate networks. First, CLIs provide unstructured data. To parse through CLI, custom regular expressions must be developed and tested for each scenario.

NETCONF overcomes these issues by using structured data (XML) and the YANG data modeling language to model to common configuration syntax and operational state to ensure a consistent API across different platforms.

In the next part of the exercise, we shall provide an overview of the relationship between NETCONF, SNMP, RESTCONF, and REST.