Skip updating port setting if no value is entered

eroche
Getting noticed

Skip updating port setting if no value is entered

So updating a script I did where I am updating a switchport settings as a whole instead of individually updating on each setting.

 

Say I want to update the following (names not correct syntax) :

  • name
  • enabled
  • poe enabled
  • vlan
  • port type
  • allowed vlans

 

If I select Access as port type it will skip the allowed vlans question but when I go the list out what I entered to verify as well as the updateDeviceSwitchPort command I get an error that allowed_vlans is blank and has no value assigned.

 

So is it possible to somehow skip the setting that are blank and not try to list or update?

 

Here is my code. API is an environment variable and switch SN is hardcoded but removed.

Thanks in Advance!

 

 

import meraki
import os
import time
import pprint
from colorama import init as colorama_init
from colorama import Fore, Back
from colorama import Style
import inquirer
from inquirer.themes import GreenPassion
import progressbar

def get_meraki_api_key():
    api_key = os.getenv('MERAKI_API_KEY')
    if not api_key:
        raise ValueError("No API key found. Please set the MERAKI_API_KEY enviroment variable.")
    return api_key



# Select Device    
sw_sn = '<sw sn>'
port = input ('\n' + 'What port would you like to update? (1,2...48): ')
print (Fore.BLUE,Style.BRIGHT +'\n' + 'Below is the current information for that port:  ' + '\n')
API_KEY = get_meraki_api_key()
dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)
serial = (sw_sn) 
port_id = (port)

response = dashboard.switch.getDeviceSwitchPort(serial, port_id)
    
fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'accessPolicyType','isolationEnabled', 'rstpEnabled', 'stpGuard', 'linkNegotiation']
    
for field in fields:
    print (f"{field}: {response[field]}")
        
# Update settings   
def update_values():
    print ('----------------------------------------------------------------' + Style.RESET_ALL)
    print (Fore.YELLOW,Style.BRIGHT +'\n' + 'Please enter the updated values, which are Case-Sensitive:  ' + '\n')
    port_name = input ('\n' + 'Port Name:  ')
    enable = input ('Enable Port [True or False]:  ')
    port_type = input ('Port Type [Trunk or Access]:  ')
    d_vlan = input ('Data VLAN:  ')
    if port_type == ('Access'):
        v_vlan = input ('Voice VLAN:  ')
    if port_type == ('Trunk'):
        v_allowed = input ('Allowed VLANs [1,3 or 1-3 or All]: ')
    rstp_enabled = input ('RSTP Enable [True or False]:  ')
    stp_guard = input ('STP Guard [Disabled, Root Guard, BPDU guard, Loop Guard]:  ')
    access_policy = input ('Access Policy [Open, MAC Allowed, Stickey MAC MAC Allowed, Internal DOT1x]:  ')
    if access_policy == ('MAC Allowed'):
        mac_allowed = input ('MAC Address [xx:xx:xx:xx:xx:xx, xx:xx:xx:xx:xx:xx): ')
    if access_policy == ('Sticky MAC Allowed list'):
        sticky_mac_allowed = input ('MAC Address [xx:xx:xx:xx:xx:xx, xx:xx:xx:xx:xx:xx): ')
        mac_allowed_limit = input ('How many MAC Address Allowed: ')
    poe_enabled = input ('PoE Enabled [True or False]:  ')


    print ('\n' + 'Here are the values you entered')
    #print ('\n' + 'Port Name: ' + port_name + '\n' + 'Port Enabled: ' + enable + '\n' + 'Port Type: ' + port_type + '\n' + 'Data VLAN: ' + d_vlan + '\n' + 'Voice VLAN: ' + v_vlan + '\n' + 'VLANs Allowed: ' + v_allowed + 'RSTP: ' + rstp_enabled + '\n' + 'STP Guard: ' + stp_guard + '\n' + 'Access Policy: ' + access_policy + '\n' + 'MACs Aloowed: ' + mac_allowed + '\n' + 'Sticky MACs Allowed: ' + sticky_mac_allowed + '\n' + 'MAC Allows Limit: ' + mac_allowed_limit + '\n' + 'PoE Enabled: ' + poe_enabled)

    validate = input ('\n' + 'Are all values correct [Y or N]:  ')
 
        
    if validate in {'Y', 'y'}:
        response = dashboard.switch.updateDeviceSwitchPort(
            serial, port_id,
            name = port_name,
            enabled = enable,
			type = port_type,
			vlan = d_vlan,
			voiceVlan = v_vlan,
			allowedVlans = v_allowed,
			rstpEnables = rstp_enabled,
			stpGuard = stp_guard,
			accessPolicyType = access_policy,
			macAllowedList = mac_allowed,
			stickyMacAllowedList = sticky_mac_allowed,
			stickyMacAllowListLimit = mac_allowed_limit,
			poeEnabled = poe_enabled,
				
            )
                
        response = dashboard.switch.getDeviceSwitchPort(serial, port_id)
    
        fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'isolationEnabled', 'rstpEnabled', 'stpGuard', 'accessPolicyType'
		  , 'macAllowedList', 'stickyMacAllowListLimit', 'linkNegotiation']
    
        for field in fields:
            print ('\n' + 'Here are the New Settings: ' + '\n')
            print (f"{field}: {response[field]}")
        input  ('\n' + 'Press Enter to return to Main Menu: ')
#        main_menu()
    
    elif validate in {'N', 'n'}:
        input ('\n' + 'Darn, Ok lets try this again, press Enter: ')
        clearconsole()
        print ('\n' + 'Current Values:' + '\n')
            
        response = dashboard.switch.getDeviceSwitchPort(serial, port_id)
    
        fields = ['portId', 'name', 'enabled', 'poeEnabled', 'type', 'vlan', 'voiceVlan', 'allowedVlans', 'isolationEnabled', 'rstpEnabled', 'stpGuard', 'linkNegotiation']
    
        for field in fields:
            print (f"{field}: {response[field]}")
                
        update_values()
            
    else:
        print ('you need to select Y,y,N or n')
            
            
update_values()

 

 

 

0 Replies 0
Get notified when there are additional replies to this discussion.