07-27-2022 07:24 AM
My code is about creating a VLAN and if the VLAN is already in the switch i want to stop (quit)
The problem is that the code gets implemented only on one switch not all
My switch list looks like this:
{
"store1": "172.22.1.97",
"store2": "172.22.1.80",
"store3": "172.22.3.12"
}
How to quit when the VLAN is already on the switch but still continue test other switches?
from netmiko import ConnectHandler
from getpass import getpass
import json
import os
import sys
password = getpass()
which_vlan = input("wich VLAN do you want to check?: ")
vlan_name = input("vlan name?: ")
working_dir = os.getcwd()
try:
with open(working_dir + '/devices_file.json', 'r') as json_file:
ip_list = json.load(json_file)
except:
print('No ip list found')
for ip in ip_list.values():
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': 'cisco',
'password': password,
'secret': 'cisco'
}
net_connect = ConnectHandler(**device)
print('Connecting to ' + ip)
net_connect.enable()
show_vlan_command = net_connect.send_command('show vlan b', use_textfsm=True)
for i in show_vlan_command:
if i['vlan_id'] == which_vlan:
print(quit('VLAN is already there'))
07-27-2022 07:57 AM - edited 07-27-2022 07:59 AM
Hi there,
I would change :
print(quit('VLAN is already there'))
to:
print('VLAN is already there')
break
As using the quit() function is causing your program to exit after the first iteration of your for loop. Using a break will exit your show_vlan_command for loop and then reach the end of the ip_list for loop. You will then iterate the ip_list a subsequent time.
cheers,
Seb.
07-27-2022 02:05 PM - edited 07-27-2022 02:06 PM
break worked very well,
but what if i have other commands that comes after to create a new VLAN,
in that situation break will break the loop but the code will continue creating the VLAN even if it is already there.
My question here is: how to break (if the VLAN is already there) the loop and not continue but start over by taking the next IP from the list and test again?
from netmiko import ConnectHandler
from getpass import getpass
import json
import os
import sys
password = getpass()
which_vlan = input("wich VLAN do you want to check?: ")
vlan_name = input("vlan name?: ")
working_dir = os.getcwd()
try:
with open(working_dir + '/devices_file.json', 'r') as json_file:
ip_list = json.load(json_file)
except:
print('No ip list found')
for ip in ip_list.values():
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': 'cisco',
'password': password,
'secret': 'cisco'
}
net_connect = ConnectHandler(**device)
print('Connecting to ' + ip)
net_connect.enable()
show_vlan_command = net_connect.send_command('show vlan b', use_textfsm=True)
for i in show_vlan_command:
if i['vlan_id'] == which_vlan:
print('VLAN is already there')
break
create_new_vlan = [
(f'vlan {which_vlan}'),
(f'name {vlan_name}'),
]
create_vlan = net_connect.send_config_set(create_new_vlan)
07-28-2022 01:32 AM
Not sure where my response has gone but presumably you have it in your email.
Jon
07-28-2022 03:19 AM - edited 07-28-2022 03:19 AM
Hi there,
Just change the logic in the show_vlan_command loop:
from netmiko import ConnectHandler
from getpass import getpass
import json
import os
import sys
password = getpass()
which_vlan = input("wich VLAN do you want to check?: ")
vlan_name = input("vlan name?: ")
working_dir = os.getcwd()
try:
with open(working_dir + '/devices_file.json', 'r') as json_file:
ip_list = json.load(json_file)
except:
print('No ip list found')
for ip in ip_list.values():
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': 'cisco',
'password': password,
'secret': 'cisco'
}
net_connect = ConnectHandler(**device)
print('Connecting to ' + ip)
net_connect.enable()
show_vlan_command = net_connect.send_command('show vlan b', use_textfsm=True)
vlan_already_there = False
for i in show_vlan_command:
if i['vlan_id'] == which_vlan:
print('VLAN is already there')
vlan_already_there = True
if not vlan_already_there:
create_new_vlan = [ f'vlan {which_vlan}', f'name {vlan_name}' ]
create_vlan = net_connect.send_config_set(create_new_vlan)
cheers,
Seb.
07-29-2022 07:00 AM
Thank you Seb and Jon.
Code is working now.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide