Have you ever found yourself ssh to MD one by one to find something? Rest assured I have an app (Python 3 code) to do this automatically.
I am not a programmer. Before this code, my programming skill is limit to write Python to print “Hello World!” so feel free to make suggestions.
I use Python 3 for this code. If your Windows 10 update to current version 2004, Python 3 builds in with Windows Subsystem for Linux, like running Python in windows cmd.
# This program will connect to each MDs in the list
# and execute the cli command in function "do_something"
from datetime import datetime
from netmiko import ConnectHandler
WC01 = {
'device_type': 'aruba_os',
'host': '1.1.1.85',
'username': 'me',
'password': 'Aruba123',
}
WC02 = {
'device_type': 'aruba_os',
'host': '1.1.1.87',
'username': 'me',
'password': 'Aruba123',
}
WC03 = {
'device_type': 'aruba_os',
'host': '1.1.1.66',
'username': 'me',
'password': 'Aruba123',
}
WC04 = {
'device_type': 'aruba_os',
'host': '1.1.1.241',
'username': 'me',
'password': 'Aruba123',
}
WC05 = {
'device_type': 'aruba_os',
'host': '1.1.1.22',
'username': 'me',
'password': 'Aruba123',
}
WC06 = {
'device_type': 'aruba_os',
'host': '1.1.1.23',
'username': 'me',
'password': 'Aruba123',
}
VMM = {
'device_type': 'aruba_os',
'host': '1.1.1.230',
'username': 'me',
'password': 'Aruba123',
}
#
# Function connect to a device in the list of all_devices
#
def connect( a_device ):
try:
device = ConnectHandler ( **a_device )
print ( 'Connected to', a_device ['host'])
return True
except:
print ( 'Not Connect' )
return False
#
# Change "command" to do any cli for each controller
#
def do_something ( a_device ):
command = 'show ap database local'
device = ConnectHandler ( **a_device )
print ( device.find_prompt(), command )
output = device.send_command ( command )
print ( output )
#
# ALWAYS disconnect from the device when we are done
#
def disconnect ( a_device ):
try:
device = ConnectHandler ( **a_device )
device.disconnect ()
print ( 'Disconnected
' )
except:
print ( 'Not disconnect
' )
return False
#
def main():
all_devices = [ WC01, WC02, WC03, WC04 ]
start_time = datetime.now()
for device in all_devices:
if connect ( device ):
do_something ( device )
print ( '#' * 80 )
disconnect ( device )
end_time = datetime.now()
execution_time = end_time - start_time
print( 'Total execution time: ', execution_time )
#
if __name__ == "__main__":
main()