cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
393
Views
0
Helpful
1
Replies

Loop to check dictionary object for key

AFlack20
Level 1
Level 1

This is a python script I'm making to run against a network switch to check if do1x has been enabled on access ports that aren't in vlan 77

I have a dictionary object that has key's nested with-in key's (don't know the correct term for this). I need to loop through this dictionary object to check for multiple conditions of keys and values but if one key is missing I need to print out the mid level key. I've provided an shortened example of the dictionary object and the code that I'm trying to get working. Any advice is greatly appreciated.

pprint.pprint(data1)
{'interfaces': {'GigabitEthernet1/0/1': {'dot1x_pae_authenticator': True,
                                         'switchport_access_vlan': '22',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/2': {'switchport_access_vlan': '77',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/3': {'switchport_mode': 'trunk'},
                'GigabitEthernet1/0/4': {'switchport_access_vlan': '55',
                                         'switchport_mode': 'access'}}}
for x in range(1, 5):
    try:
        if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['switchport_mode'] == 'access':
            if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['switchport_access_vlan'] != '77':
                if data1['interfaces']['GigabitEthernet1/0/' + str(x)]['dot1x_pae_authenticator']:
                    pass
        else:
            print('int GigabitEthernet1/0/' + str(x))
    except Exception:
        pass

My goal is to get it to print int GigabitEthernet1/0/4 but instead I get int GigabitEthernet1/0/3. I don't care about gig 1/0/3 because its a trunk.

1 Reply 1

Marcel Zehnder
Spotlight
Spotlight

Hi 

 

I don't understand what you mean by 'if one key is missing I need to print out the mid level key'. But to achieve your goal - check if dot1x has been enabled on access ports that aren't in vlan 77 - you can do the following:

import pprint

data1 = {'interfaces': {'GigabitEthernet1/0/1': {'dot1x_pae_authenticator': True,
                                         'switchport_access_vlan': '22',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/2': {'switchport_access_vlan': '77',
                                         'switchport_mode': 'access'},
                'GigabitEthernet1/0/3': {'switchport_mode': 'trunk'},
                'GigabitEthernet1/0/4': {'switchport_access_vlan': '55',
                                         'switchport_mode': 'access'}}}

not_vl77_dot1x = []

for intf in data1['interfaces'].keys():
    if 'dot1x_pae_authenticator' in data1['interfaces'][intf]:
        if data1['interfaces'][intf]['dot1x_pae_authenticator'] and data1['interfaces'][intf]['switchport_mode'] == 'access':
            if data1['interfaces'][intf]['switchport_access_vlan'] != '77':
                not_vl77_dot1x.append(intf)

pprint.pprint(not_vl77_dot1x)

 

HTH

Marcel