I am wondering how I can iterate through 48 ports of a SAN switch one by one and enable or disable them ? For some background info, I write python scripts for work to automate the testing of networking devices. I use a library called paramiko which connects to these devices using SSH.
Here is a simple function I wrote in where a user enters in which port they want to disable
def disablePort(ssh):
user_input = input("Enter the port number you want to disable\n")
channel = ssh.invoke_shell()
ssh.exec_command("portdisable " + user_input)
channel.close()
print("Port " + user_input + " " + "disabled\n")
print("Waiting 10 seconds as instructed by the test case\n")
time.sleep(10)
Now, I got 48 ports on this switch and I wish to disable them one by one. I feel like a naive solution would be to create a list where all the port names are hardcoded and iterate through each one and feed it in to the ssh.exec_command() method, but is there a more elegant/practical solution to this ? How would I be able to go about doing this ? Thanks for the help!
You should be able to connect with Paramiko first, then run something like (for cisco)
show interfaces status
and read the stdout from paramiko to get all the interfaces. You can then use that to iterate over, the first column being the names.
Once you have a list of the ports, you can loop over them one by one and disable them in the same manner. For example
for port in myListOfPorts:
ssh.exec_command(f"portdisable {port}")
Though, you should check out Netmiko, it's a port of paramiko for network devices. https://github.com/ktbyers/netmiko
Lastly, there's probably a single command to tear down all the interfaces instead of doing them one at a time.