BLOGPOST#151 Nexus 9000 V Image Installation in eve-ng: A Comprehensive Guide to Enabling API in NXOS and Python Integration

Introduction:

Python is one of the most versatile programming languages that is used widely by developers and network engineers. It is easy to learn and implement, which makes it a popular choice among beginners. This article focuses on installing the Nexus 9000 V image into an eve.ng environment and how to enable API and initiate API connection from Python.

Installing Nexus 9000 V image into an eve.ng environment:
To install Nexus 9000 v image into eve.ng environment, we need to download the Nexus qcow2 image from the official site. After downloading the image, we need to verify its MD5 using the sum and image name. Once the verification is successful, we need to rename the file to ‘sata.qcow2’, as per the naming convention. After creating a directory with the correct name, we can copy the image file to eve.ng using any copying mechanism like SFTP.

Enabling API:
After copying the image file to eve.ng, we need to set the permission to run the file using the search command. Once the copying is completed, we can enable the API using the feature nxAPI command. Before saving the configuration, we need to set the boot using the boot flash command.

Initiating API connection from Python:
Application Programming Interface (API) is a communication protocol between two different applications. APIs are used to exchange data between two applications. It is widely used in the software industry for developing applications, especially in cloud computing. In this article, we will learn how to initiate an API connection from Python.
To initiate API connection from Python, we need to import the requests module and send an HTTP GET request to the Nexus 9000 API. We can use the requests.get() method to send the request and get the output.

To initiate an API connection from Python, we will need to follow the below steps:

Import the required modules:
In Python, we will need to import the required modules for initiating an API connection. The requests module is used to send HTTP requests using Python. The json module is used to handle the JSON format data

import requests
import json
from pprint import pprint

Define the API URL:
After importing the required modules, we need to define the API URL to which we want to connect. We can also pass the parameters to the API URL if required.

url=’https://192.168.0.201/ins’

Define the headers and authentication details: 
We need to define the headers and authentication details to initiate the API connection. Headers provide information about the request like the type of data being sent, authorization details, etc. Authentication details are used to authenticate the user who is accessing the API.

myheaders={'content-type':'application/json-rpc'}
payload=[
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "show hardware",
      "version": 1
    },
    "id": 10
  }
]

Initiate the API connection: 
We can initiate the API connection using the requests module. We can pass the URL, headers, and authentication details to the requests module’s get or post method based on the API’s HTTP method.

response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword), verify=False).json()

Handle the response: 
After initiating the API connection, we need to handle the response we receive from the API. We can check the status code of the response to ensure that we received a successful response from the API. We can also parse the response data using the json module.

pprint(response)
print(response['result']['body']['kickstart_ver_str'])
print(response['result']['body']['kick_file_name'])

Conclusion:
In this article, we saw how to install the Nexus 9000 V image into an eve.ng environment, enable API, and initiate API connection from Python. Python is an easy-to-learn programming language that can be used by network engineers to automate their tasks. With the help of Python, we can easily connect to the Nexus 9000 API and get the desired output.

Complete Script for Reference:

#! /usr/local/Python_envs/Python3/bin/python3
import requests
import json
from pprint import pprint

from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
"""
Modify these please
"""

switchuser='admin'
switchpassword='admin'

url='https://192.168.0.201/ins'
myheaders={'content-type':'application/json-rpc'}
payload=[
  {
    "jsonrpc": "2.0",
    "method": "cli",
    "params": {
      "cmd": "show hardware",
      "version": 1
    },
    "id": 10
  }
]

response = requests.post(url,data=json.dumps(payload), headers=myheaders,auth=(switchuser,switchpassword), verify=False).json()

pprint(response)
print(response['result']['body']['kickstart_ver_str'])
print(response['result']['body']['kick_file_name'])

Unlock the Power of Network Automation ! Enroll in our Comprehensive Udemy Course Today !

Topics & libraries Covered:

  • PARAMIKO, NETMIKO, NAPALM, NORNIR and NCCLIENT libraries
  • Cisco IOS, vIOS and NXOS SSH Automation
  • Cisco CSR 1000v Netconf and RestAPI examples
  • NX-API Automation (NXAPI CLI and bash)
  • PyATS, Cisco Genie Parser
  • NETCONF & RESTCONF API Automation
  • YANG Data Models and YANG Suite demo
  • IPAddress Module and requests module
  • Python Core Fundamentals for Network Engineers
  • Python Data Types
  • Python Text & CSV File operations (read/write) device data
  • Python IDE (PyCharm) Setup for Network Automation
  • GNS3 Lab Setup for Network Automation
  • Python Functions, Modules, Classes and Objects Tutorial
  • Python Multithreading Examples (threading and concurrent futures)
  • Python Logging, schedule and Email for monitoring
  • Python SSH CLI Parsing Using RegEx
  • How to Use JSON, YAML and XML Files for Device Config