cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
801
Views
5
Helpful
4
Replies
Highlighted
Beginner

Nexus Interface Bulk/Range Selection

Hello,

 

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

Everyone's tags (2)
4 REPLIES 4
VIP Advisor

Re: Nexus Interface Bulk/Range Selection

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:

https://www.cisco.com/c/en/us/td/docs/switches/datacenter/nexus5000/sw/programmability/guide/b_Cisco_Nexus_5K6K_Series_NX-OS_Programmability_Guide/python_api.html

 

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

 

cheers,

Seb.

Beginner

Re: Nexus Interface Bulk/Range Selection

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.

VIP Advisor

Re: Nexus Interface Bulk/Range Selection

I've come across this:

https://github.com/hoelsner/python-script-examples/blob/master/cisco-nx-api-example/interface-description-cleaner.py

 

...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... :)

 

cheers,

Seb.

VIP Advisor

Re: Nexus Interface Bulk/Range Selection

OK here you go:

#!/usr/bin/env python3
"""
NXAPI script inspired by this question:
  https://supportforums.cisco.com/t5/unified-computing-system/nexus-interface-bulk-range-selection/m-p/3217057
"""
import json
import requests

nx_hosts = ["10.209.31.67", "10.209.31.100"]
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)
    try:
        response = requests.post(url,
                                 data=json.dumps(payload),
                                 headers=headers,
                                 auth=(nx_username, nx_password),
                                 timeout=4)
    except requests.exceptions.ConnectTimeout as exc_info:
        print(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.')
        else:
            print('HTTP Status Code {code}. Reason: {reason}'.format(
                code=exc_info.response.status_code,
                reason=exc_info.response.reason))
        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)

    print(response)


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
    else:
        return None


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

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


if __name__ == "__main__":
    main()

I will maintain an updated version here:

https://github.com/sebrupik/srupik-nxos-tools/blob/master/nxapi/conf_interface.py

 

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 :)

 

cheers,

Seb.

 

Everyone's tags (3)
CreatePlease to create content
Content for Community-Ad
August's Community Spotlight Awards