I find myself needing to refresh our older switches (of which there are several) We are, of course, a Cisco shop and have several Data Rooms, with about twenty plus switches in each. Weather your running this against 3750’s or 9300’s this little python function will help you quickly asses which ports are configured to which Vlans for your entire setup.
I used this to get a port inventory, however it can be easily modified to run any Cisco IOS command in bulk.
Requirements:
- Workstation with at least Python 3.11 installed which has SSH access (port 22) to your switches.
- The ability to import the (God-Tier) Netmiko library.
- Username and password for switches with at least read privileges.
# Inventorys Cisco Switch ports and writes to CSV file
from __future__ import print_function
import os
import glob
from contextlib import redirect_stdout
from netmiko import ConnectHandler, NetmikoAuthenticationException, NetMikoTimeoutException, SSHDetect
# TODO Populate the list with the IP addresses of the Switches you would like to inventory
ger_ip_list = [
'<ip address of switch>',
]
# TODO Specify Older Algorithms to disable (two below will most likely help with older IOS devices)
disabled_algorithms = {'pubkeys': ['aes256-cbc']}
username = '<username for SSH on Switches>'
password = str("<password for SSH on Switches")
device_type = 'cisco_ios'
def ger_switch_inventory():
# TODO Specify Directory for Program Files
dir_pattern = 'port_data/*.csv'
for file_path in glob.glob(dir_pattern):
if os.path.isfile(file_path):
os.remove(file_path)
for ip in ger_ip_list:
device = ConnectHandler(device_type=device_type,
ip=str(ip),
username=username,
password=password,
auth_timeout=90,
timeout=10)
output = device.send_command('terminal length 0')
output = device.send_command('enable')
output = device.send_command('sh int status')
with open(f'port_data/GER/{ip}.sh_int.csv', mode="x") as file:
with redirect_stdout(file):
print(output)
print(output)
ger_switch_inventory()