11-15-2017 05:02 AM - edited 03-01-2019 01:21 PM
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
11-15-2017 05:21 AM
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!
cheers,
Seb.
11-15-2017 07:21 AM
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.
11-15-2017 07:34 AM
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... :)
cheers,
Seb.
11-16-2017 01:43 AM - edited 11-16-2017 03:46 AM
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.
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: