cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
2331
Views
6
Helpful
4
Replies

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

What In the
Level 3
Level 3

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

aleabrahao
Meraki Community All-Star
Meraki Community All-Star

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 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.

obrigg
Meraki Employee All-Star
Meraki Employee All-Star

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".

Thank you.

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

obrigg
Meraki Employee All-Star
Meraki Employee All-Star

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.