Showing results for 
Search instead for 
Did you mean: 

Nexus Interface Bulk/Range Selection




I'm working on several 5Ks, each having several FEXs attached.


I need to select a lot of "notconnected" ports, place them in a non routed VLAN, and shut them down.


The problem: The ports are all over the place. The Nexus range feature is much improved but not that helpful in this scenario, as the sheer tedium and possibility of error are too great.


I'm looking at port-profiles. This is a nice feature, but it would still be necessary to manually hunt and peck through a ton of random disconnected ports.


Is there no way to select interfaces on anything other than their type and number? Selection based on status would be nice.


Thank you


Seb Rupik
VIP Advisor VIP Advisor
VIP Advisor

Hi there,

This is a perfect situation to look at the NX-OS on-box python API.

Regex the output of 'sh int status' matching on 'notconnected', extract the interface ID and add it to a list. Finally iterate over the list, applying the desired commands.


Take a look here:


If you've not done any python programming, in this instance it will probably be quicker to do it manually!




This is a nice recommendation. Thank you.


Have you(or anyone out there) done this? Would you be willing to supply the code as a template.


I have to believe that this would benefit many.


Thanks again.

I've come across this:


...which you could probably bend into shape.

The json structure 'result' returned from nxapi_cli_show() will have a different structure, but once you work out the keys, you should be able to return a string for each interface in a 'notconnected' state. Parse that string and extract the interface ID. Later on in the script you can see the 'change script' being constructed, this is where your desired commands would go.


Unless someone beats me to it, give me 24 hours and I'll see if I can write something... :)




OK here you go:

#!/usr/bin/env python3
NXAPI script inspired by this question:
import json
import requests

nx_hosts = ["", ""]
nx_username = "admin"
nx_password = "password"
nx_port = "8080"

headers_jsonrpc = {"content-type": "application/json-rpc"}
headers_json = {"content-type": "application/json"}

int_target_state = "sfpAbsent"

non_routed_vlan = 5
int_command = "int {0}; switchport access vlan {1}; switchport mode access; shutdown; "

def nxapi_request(host, payload, headers):
    url = "http://{0}:{1}/ins".format(host, nx_port)
        response =,
                                 auth=(nx_username, nx_password),
    except requests.exceptions.ConnectTimeout as exc_info:
        return None
    except requests.exceptions.HTTPError as exc_info:
        if exc_info.response.status_code == 401:
            print('Authentication Failed. Please provide valid username/password.')
            print('HTTP Status Code {code}. Reason: {reason}'.format(
        return None

    return response.json()

def apply_config_commands(host, config_commands):
    payload = {"ins_api": {"version": "1.2",
                           "type": "cli_conf",
                           "chunk": "0",
                           "sid": "1",
                           "input": config_commands,
                           "output_format": "json"}}

    response = nxapi_request(host, payload, headers_json)


def create_config_commands(host):
    payload = [
        {"jsonrpc": "2.0", "method": "cli", "params": {"cmd": "show int status", "version": 1.2}, "id": 1},
    config_commands = ""

    response = nxapi_request(host, payload, headers_jsonrpc)
    if response is None:
        return None

    for interface in response["result"]["body"]["TABLE_interface"]["ROW_interface"]:
        if interface["state"] == int_target_state:
            config_commands += int_command.format(interface["interface"], non_routed_vlan)

    if config_commands.endswith("; "):
        config_commands = config_commands[:-2]

    if len(config_commands) > 0:
        return config_commands
        return None

def main():
    for host in nx_hosts:
        print("Trying {0}".format(host))

        config_commands = create_config_commands(host)
        if config_commands is not None:
            apply_config_commands(host, config_commands)

if __name__ == "__main__":

I will maintain an updated version here:


There is a small amount of error checking, but I wanted to keep the script brief, so it may just bomb out if things are not just-so.


Please note, I haven't tested the applying of config as I don't have a dev Nexus to play with. I've run it against production switches as far as building the 'config_commands' string, which looks correct.

You should also change the value of the variable 'int_target_state' to "notconnected" and also 'non_routed_vlan' .


Let us know how it goes :)





Getting Started

Find answers to your questions by entering keywords or phrases in the Search bar above. New here? Use these resources to familiarize yourself with the community: