<img height="1" width="1" style="display:none" src="https://www.facebook.com/tr?id=352585001801011&amp;ev=PageView&amp;noscript=1">
Igor Parsadanov

By: Igor Parsadanov on March 16th, 2016

Print/Save as PDF

Cisco Nexus 9000 Automation, Part 2

Modern Data Center

(This post provides a continuation to Matt Kozloski’s post about Nexus 9000 Automation. In his post, Matt provides a primer on Nexus 9000 and shows some example code using PowerShell to access the REST API on the Nexus 9000.)

Cisco Nexus 9000We already know that the Nexus 9000 is an awesome switch, before we even get to the goodness of its programmability features. For example, the Cisco Nexus 9372PX performs non-blocking 10G line rate switching, has 6x 40GbE uplinks, and it sports a rich set enterprise functions you would expect from a Cisco Nexus switch, such as L2 switching, L3 routing, VXLAN, VPC, and Fabric Extenders to name a few.

 

In this exercise we are going to query the switch for some information remotely using Python.

1.) First things first, you’ll need to have a Python Interpreter installed. You can download and install Python Interpreter from https://www.python.org/. This example uses Python 2.7.11.

2.) Next, let’s enable the NX-API on the switch. To do that we use one command feature nxapi under global configuration mode.

 

Cisco_Nexus_9000_python_automation_screengrab_1.png

 

3.) From here we are able to use the RESP API along with accessing the NX-API sandbox. Matt’s post talks more about the Sandbox.

4.) Let’s dive into the code, our very own "Hello World" example:

Some header and environment setup:

import json
import requests

my_headers = {'content-type': 'application/json-rpc'}
url = "https://172.16.55.100/ins"
username = "cisco"
password = "cisco"
#next line is to disable warnings about self-signed SSL cert
requests.packages.urllib3.disable_warnings()

payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh ver",
                     "version": 1},
          "id": 1}
         ]

response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()

print "\nHostname: " + response['result']['body']['host_name']
print "System Type: " + response['result']['body']['chassis_id']
print "Version: " + response['result']['body']['rr_sys_ver']

 

4.) Since we have our result stored as json encoded object, we can access the fields of the object. The three print statements output the Hostname, System Type, and NX-OS version.

5.) Now that we are armed with how to return values out of a json object we can move forward using the sandbox to figure out how the output fields are coded. In the rest of the example, we extract information about environmental sensors, interfaces, routing table, OSPF. The possibilities are endless as long as we can CLI it, we can programmatically access the information using the API, perform automated logic statements, and issue more CLI statements.

Cisco_Nexus_9000_python_automation_screengrab_2.png

 

Full code:

#Print Switch Details
import json
import requests

my_headers = {'content-type': 'application/json-rpc'}
url = "https://172.16.55.100/ins"
username = "cisco"
password = "cisco"
requests.packages.urllib3.disable_warnings()

#basics
payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh ver",
                     "version": 1},
          "id": 1}
         ]

response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()

print "\nHostname: " + response['result']['body']['host_name']
print "System Type: " + response['result']['body']['chassis_id']
print "Version: " + response['result']['body']['rr_sys_ver']




#ip stuff


payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh ip int br",
                     "version": 1},
          "id": 1}
         ]



response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()
TABLE_intf=response['result']['body']['TABLE_intf']
for interface in TABLE_intf:
  print "\nInterface Name: " + interface['ROW_intf']['intf-name'] + " : " + interface['ROW_intf']['prefix']





#environmentals



payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh environment",
                     "version": 1},
          "id": 1}
         ]

response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()


powersupply = response['result']['body']['powersup']['TABLE_psinfo']['ROW_psinfo']
print "\nNumber of Power Supplies:" + str(len(powersupply))
for i in powersupply:
    print "PS: " + str(i['psnum']) + " " + i['ps_status']
    
temperature = response['result']['body']['TABLE_tempinfo']['ROW_tempinfo']
print  "Current Intake Temperature: " + temperature[0]['curtemp']


#ospf stuff

payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh ip ospf nei",
                     "version": 1},
          "id": 1}
         ]

response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()

neighbors = response['result']['body']['TABLE_ctx']['ROW_ctx']['nbrcount']

print "\nNumber of OSPF neighbors: " + str(neighbors)
print "Neighbors:"
print "RID             IP             Interface"
for i in range(0,neighbors):
    row = response['result']['body']['TABLE_ctx']['ROW_ctx']['TABLE_nbr']['ROW_nbr'][i]
    print row['rid'] + "           " + row['addr'] + "            " + row['intf']




#route stuff

payload=[{"jsonrpc": "2.0",
          "method": "cli",
          "params": {"cmd": "sh ip route vrf all",
                     "version": 1},
          "id": 1}
         ]

response = requests.post(url, data=json.dumps(payload), headers=my_headers, auth=(username, password), verify=False).json()

print "\nNumber of VRFs: " + str(len(response['result']['body']['TABLE_vrf']['ROW_vrf']))
for vrf in response['result']['body']['TABLE_vrf']['ROW_vrf']:
    print "VRF name: " + vrf['vrf-name-out']
    print "Number of Routes: " + str(len(vrf['TABLE_addrf']['ROW_addrf']['TABLE_prefix']['ROW_prefix']))

 

6.) Congratulations on automating your switch with Python!

As I mentioned earlier, the Cisco Nexus 9000 is an awesome, affordable, enterprise-class switch. If you're considering projects such as upgrading to 10Gb Ethernet, introducing HyperConverged into your environment, and refreshing your storage network, give us a call or send us a note. We'd be happy to talk with you about how you can revolutionize your datacenter fabric.

About Igor Parsadanov

Igor is a veteran data center architect. CCIE # 48535.