API not working all the sudden - cycle ports in specific vlan

Capt_Caaaveman
Getting noticed

API not working all the sudden - cycle ports in specific vlan

I got the below working yesterday.  now today it just keeps cycling the same port over and over.

I was hoping someone could assist in where I made a mistake or help tweak this.

 

I am trying to cycle any port in a defined net id that is set to the target vlan.

 

 

import meraki

target_vlan = 5

# Create Meraki API Dashboard interface
dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)

devices = dashboard.organizations.getOrganizationDevices(ORG_ID, total_pages='all', networkIds=[NET_ID])
for device in devices:
    if device['productType'] == 'switch':
        device_ports = dashboard.switch.getDeviceSwitchPorts(device['serial'])

# Filter ports by VLAN
ports_to_cycle = []
for port in device_ports:
    if port['vlan'] == target_vlan:
        ports_to_cycle.append(str(port['portId']))

    # Cycle the selected ports
    if ports_to_cycle:
        dashboard.switch.cycleDeviceSwitchPorts(device['serial'], ports_to_cycle)
        print(f"Successfully cycled ports {ports_to_cycle} on switch {device['name']} in VLAN {target_vlan}")
    else:
        print(f"No ports found in VLAN {target_vlan} on switch {device['name']}")

 

4 Replies 4
alemabrahao
Kind of a big deal

Hi,

 

You're calling cycleDeviceSwitchPorts() inside the loop that checks each port, which means it could be triggered multiple times unnecessarily, especially if multiple ports match the VLAN.

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
Oren
Meraki Employee
Meraki Employee

Could this be an indentation problem?
"ports_to_cycle" does not seem to be under the "for device in devices:" loop, which would mean only the last device/serial number would make it into "ports_to_cycle".

Capt_Caaaveman
Getting noticed

Thank you.

 

It was an indent issue.  Not sure how it changed.  I think PyCharm prompted me about format change  and I selected that.  

Oren
Meraki Employee
Meraki Employee

Great to hear!

While you’re at it, you can consider using getOrganizationDevices specifying productTypes=“switch”, which will retrieve only switches (saving you to filter the results afterwards).

Or, skip two steps by leveraging getOrganizationSwitchPortsBySwitch which will get you the port configurations of all switches in the organization.

 

In a small environment it won’t matter much, but in larger ones - it would be a ~80% API call reduction.

Get notified when there are additional replies to this discussion.