05-21-2025 09:09 AM
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']}")
05-21-2025 09:33 AM
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.
05-21-2025 09:43 AM
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".
05-22-2025 07:47 AM
Thank you.
It was an indent issue. Not sure how it changed. I think PyCharm prompted me about format change and I selected that.
05-22-2025 07:55 AM
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.
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