cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
586
Views
15
Helpful
5
Replies

Python, quit loop and start with next element

Moudar
VIP
VIP

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'))

 

5 Replies 5

Seb Rupik
VIP Alumni
VIP Alumni

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.

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)
   
        

 

 

 

 

Not sure where my response has gone but presumably you have it in your email. 

 

Jon

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.

Thank you Seb and Jon.

Code is working now.

Review Cisco Networking for a $25 gift card