Using REST to update interface descriptions with CDP neighbors

The below script was used on Nexus 9000v NX-API which can be enabled with ‘feature nxapi’

I’m using the same topology from the VXLAN post:

#!/usr/bin/python
import requests
import json

def updateDescriptions( server ):
 session = requests.session()
 session.auth = ("admin","admin")
 # Creating the payload to send "show cdp neighbor detail" to NX-API
 payload = { "ins_api" : {
                            "version" : "1.0",
                            "type" : "cli_show",
                            "sid" : "1",
                            "chunk" : "0",
                            "input" : "show cdp nei detail",
                            "output_format" : "json"
                            }
             }
  try:
    response = session.post("http://" + server + "/ins",data=json.dumps(payload))
    # Loading the CDP neighbors into a list
    data = json.loads(response.text)["ins_api"]["outputs"]["output"]["body"]["TABLE_cdp_neighbor_detail_info"]["ROW_cdp_neighbor_detail_info"]

  except requests.exceptions.Timeout:
    print server + " has timed out"
  except requests.exceptions.RequestException:
    print server + " connection failed"




  # Looping through the cdp neighbor list
  for key in data:
    my_interface = key["intf_id"]
    neighbor_name = key["device_id"]
    neighbor_ip = key["v4addr"]
    # Creating the payload to push the cdp neighbor info to the interface description
    config_payload = { "ins_api" : {
                                    "version" : "1.0",
                                    "type" : "cli_conf",
                                    "sid" : "1",
                                    "chunk" : "0",
                                    "input" : "interface %s ; description %s (%s)"
                                               % (my_interface,neighbor_name,neighbor_ip),
                                    "output_format" : "json"
                                    }
                        }
    try:
      push = session.post("http://" + server + "/ins",data=json.dumps(config_payload))
    except requests.exceptions.Timeout:
      print server + " has timed out"
    except requests.exceptions.RequestException:
      print server + " connection failed"

servers = ["192.168.255.1","192.168.255.2","192.168.255.3","192.168.255.4"]
for ip in servers:
    updateDescriptions(ip)

Verify

# Before
# LEAF-1
-------------------------------------------------------------------------------
Port          Type   Speed   Description
-------------------------------------------------------------------------------
Eth1/1        eth    10G     --
Eth1/2        eth    10G     --


# After
# LEAF-1
-------------------------------------------------------------------------------
Port          Type   Speed   Description
-------------------------------------------------------------------------------
Eth1/1        eth    10G     SPINE-1(9S7VTM2OG4Z) (192.168.0.13)
Eth1/2        eth    10G     SPINE-2(9U7PYVIGKC3) (192.168.0.21)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.