Dev: get_vlans.v1
Hi there, this a basic script that connects to a list of switches, get the vlans, vlans name, interfaces where the vlan is assigned and the total number of vlans.
from netmiko import ConnectHandler
# connect to devices
for n in range(1,5):
ip = "192.168.234.4{0}".format(n)
device = {
'host': ip,
'username': 'silesio',
'password': 'cisco',
'device_type': 'cisco_ios'
}
login = ConnectHandler(**device)
# get device hostname
hostname = login.send_command('show run | in hostname')
hostname = hostname.split(' ')
hostname = hostname[1]
print('\n*** Connecting to ' f"{hostname}" '***')
# get the vlans and parse into python as dictionary
cmd = login.send_command("show vlan brief", use_textfsm=True)
for vlan in cmd:
print(f"{vlan['vlan_id']} {vlan['name']} {vlan['interfaces']}")
# get the total of usable vlans
vlans = [k['vlan_id'] for k in cmd if k.get('vlan_id')]
total_vlans = len(vlans)-4
print(f"{hostname}" " total vlans: " f"{total_vlans}" )
And that’s it.
I hope you enjoyed this post, leave your comments below and I'll see you on the next one.
TAC Engineer
4 年You great, Bro!