#158 Initiating a Bash Connection to Nexus Devices and Executing Linux Commands via Nexus API

Introduction:

Hello everyone! In this blog, we will learn how to do Nexus API automation using Python. Specifically, we will see how to initiate a bash connection to a Nexus device using a nexus API and execute Linux commands on the Nexus device.

If you’re working with Nexus OS, you may find it helpful to use the Bash shell to execute commands and scripts. In this article, we’ll cover some basic commands and features of Bash on Nexus OS, including memory information, switching to root user, getting interface details, and more.

First, we need to enable Bash, which we can verify by using the “show” command. We can do this by executing the following command:

feature bash-shell

Once we have enabled Bash, we can access the native Linux environment of the Nexus by executing the command:

run bash

We can then execute any Linux command from the Nexus device’s bash terminal, such as:

run bash pwd

To initiate the same from the Sandbox, we need to go to JSON and execute the command using Bash. We can execute other Linux commands on the Nexus device, We can do this by executing the following command:

{
  "ins_api": {
    "version": "1.0",
    "type": "bash",
    "chunk": "0",
    "sid": "sid",
    "input": "pwd",
    "output_format": "json"
  }
}

Memory Information:
To get memory information about your device, simply type the command “memory info” in the Bash shell. This will provide you with details about the memory usage and available memory on your device.

Switching to Root User:
If you want to switch to the root user, you can do so by typing “su root” in the Bash shell. This will prompt you for a password, so enter your current user password, followed by “admin”. Once you’ve entered the correct password, you should be switched to the root user.

Interface Details:
To get all the interface details, simply execute the command “if config”. This will provide you with all the interface details on your device.

Packet Capture Capabilities:
To capture packets on your device, you can use the “tcpdump” command in the Bash shell. This will allow you to see the packet capture capabilities of your machine.

Installed Packages:
To see a list of packages installed on your device, use the command “yum list installed”. This will provide you with a list of all the packages currently installed on your device, including Python packages.

Python Environment:
To see the Python environment on your device, type “which Python3” in the Bash shell. This will display the Python3 installation. Similarly, you can also see the Python2 installation by typing “which Python2”.

Creating Files and Folders:
You can easily create files and folders in Bash on Nexus OS, get the boot flash details, and create Python scripts in the Nexus device’s bash. To create a Python script, for example, type “vi test.py” in the Bash shell. This will create a new file called test.py in the script directory. You can then edit the file as needed and save it.

Postman Integration:
If we want to run this bash command from Postman, we can execute the command and get the payload from the Nexus device. We can then copy this payload and go to Postman, where we can duplicate an existing request and rename it to “JSON Bash”. We can then paste the payload in the body and send it to the Nexus device.

{
  "ins_api": {
    "version": "1.0",
    "type": "bash",
    "chunk": "0",
    "sid": "sid",
    "input": "cat /etc/os-release",
    "output_format": "json"
  }
}

The bash commands are useful while integrating with many third-party applications, such as Ansible, OpenStack, Chef, and Puppet. We can install all of these using “yum install” in the bash. If we want to send configuration lines from Bash, we can use a virtual shell available by executing “vsh -c”. We can then execute multiple commands, each separated by a semicolon.

Virtual Shell:
The Virtual Shell is a tool available in Bash on Nexus OS that can be used to send multiple lines of configuration. To use it, type “vsh -c” in the Bash shell. This will display the command and syntax supported by the Virtual Shell. You can use it to verify with vsh -c “show run int e1/2” and remove interface configurations with vsh -c “config t ; default int e1/2”, as well as to send configuration lines.

Conclusion:
Bash on Nexus OS provides a powerful set of tools and commands for managing your device. By mastering the basics, you can execute commands and scripts, create files and folders, and integrate with other tools like Postman. With a little practice, you can become a Bash expert and take your device management to the next level.

Simple Script for Reference:

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

url = "https://192.168.0.201/ins"

payload = json.dumps({
  "ins_api": {
    "version": "1.0",
    "type": "bash",
    "chunk": "0",
    "sid": "sid",
    "input": "cat /etc/os-release",
    "output_format": "json"
  }
})
headers = {
  'Content-Type': 'application/json',
  'Authorization': 'Basic YWRtaW46YWRtaW4=',
}

response = requests.request("POST", url, headers=headers, data=payload, verify=False)

print(response.text)

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