Connecting over SSH and running a command over NETCONF

When HW/virtualized/containerized network element offers NETCONF interface to manage, it is extremely beneficial to use it for repetitive tasks (upgrades, sanity checking, route table checking etc).

NETCONF can be used over different transports as below:

In case SSH is used then it must be made sure that SSH subsystem is enabled in SSH config on a device. 

If a YANG model is available then TailF offers a client and Java class generator under:

https://github.com/tail-f-systems/JNC

Alternatively, as in the example below, Python can be used to manually connect over SSH and based on YANG model (if used) instruct the device to perform a specific task.

There is a library in Python for that purpose called ncclient (NETCONF client): 

https://github.com/ncclient/ncclient

Firstly we need to do the proper import in our client script after installing the library:

Let’s define method used for connecting to the device:

Create an object class that inherits from RPC class of ncclient library and define a method that will be compose an XML NETCONF message based on YANG model:

Connect to the device and perform requested action (probably not the safest way to use clear text password to connect):

The request in NETCONF formatted XML would look as follows:

And a corresponding YANG model:

Lastly in XML:

Installing additional packages for Python on Windows

On Windows 7 I have tried to install paramiko package so as to have possibility to SSH from Python scripts to network devices or any other SSH servers.

  1. Install setuptools https://pypi.python.org/pypi/setuptools#id3
  2. Install VC++ compiler for Python2.7 http://www.microsoft.com/en-us/download/details.aspx?id=44266
  3. Install a required package:

 VC++ compiler installation is needed as some of the downloaded files needs compilation to executable files or libraries.