Why Insomnia and not Postman?

Well, answer is straightforward – apples vs oranges. Somewhat I prefer Insomnia, though it is just a simplistic API client. But this is exactly what I normally need without additional features and Insomnia seems to be fulfilling my daily needs.

  • Depending on against what environment you run your API calls you can define a variables and their values per environment e.g. lab/prod. (Left top corner). Templating relies on Nunjucks

variable values for Dev environment

  • Allows to refer to results of different API calls e.g. Authorization Bearer token value – in the example below Fetch users requires Bearer token to be included in GET request hence it needs to perform first Authorization, obtain token and insert it into GET request:

Authorize URL looks as follows (IMPORTANT: never embed usernames, passwords in config files – rely on solutions like Hashicorp’s Vault, Azure KeyVault etc):

By peeking at results of the POST (Timeline) response from the server includes authorization Header that includes token value.

 

Subsequent Method can use the value just by manually copy pasting value of the header into authorization header of GET request:

Smarter solution is to define header in Header tab and use a function that will call POST Authorize method, grab the resulting authorization header value (Bearer token) and insert it into a header in the GET request. In value type CTRL+Space and look for function Response – Header:

Double click on red field and Edit tag window will pop up:

Now your requests will be using appropriate value for authorization header.

IMPORTANT: in order to have Edit tag pop up available you cannot enable (leave untickedRaw template syntax as otherwise you’ll have manually craft in Nunjucks value of the header.

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.