cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
7993
Views
4
Helpful
12
Replies

Python - Get list of switch ports from specific Network ID

What In the
Level 3
Level 3

Im trying to test getting info from my Org to learn building scripts.

I want to get the list of switch ports from a specific Network ID. I am able to pull the device ports if I use the switch's serial number:

response = dashboard.switch.getDeviceSwitchPorts(
serial
)

print(response)

https://developer.cisco.com/meraki/api-v1/get-device-switch-ports/

Im trying to expand upon this by getting the switch ports from a net ID

import meraki

API_KEY = 'c4............................................'

dashboard = meraki.DashboardAPI(API_KEY)

organization_id = 'XXXXXX'

# Get the list of networks in your organization
networks = dashboard.organizations.getOrganizationNetworks(
organization_id, total_pages='all'
)

# Find the network that you want
network_id = 'L_6................................'

# Get the list of switchports
switchports = dashboard.switch.getDeviceSwitchPorts(
network_id
)

# Print the list of switchports
for switchport in switchports:
print(switchport)

I get an error

ERROR > switch, getDeviceSwitchPorts - 404 Not Found, b''

12 Replies 12

aleabrahao
Meraki Community All-Star
Meraki Community All-Star

Have you tried this API?

https://developer.cisco.com/meraki/api-v1/get-organization-switch-ports-statuses-by-switch/

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.

Raphael_L
Meraki Community All-Star
Meraki Community All-Star

Hi ,

https://developer.cisco.com/meraki/api/get-device-switch-ports/

/devices/{serial}/switch/ports

You can't input a Network ID , it requires a Serial number. You would be better using the Org-Wide call that alemabrahao has suggested and filter the json received or filter with the parameter networkids.

What In the
Level 3
Level 3

Thank you both.

HMM. I was playing with this to add

# Filter the devices to only include switches
switches = []
for device in devices:
if device["model"].startswith("MS"):
switches.append(device)

# Print the list of switches
for switch in switches:
print(switch["serial"])

this gets me the serial of each switch.

Can I use this output in the operations you both have provided?

I assume I am going need to build an array or something

wellyhartanto
Level 3
Level 3

So you want to get the list of switch ports by network ID?

API_KEY = 'xxxx'

ORG_ID = 123

NET_ID = L_123

dashboard = meraki.DashboardAPI(API_KEY, suppress_logging=True)
ports = dashboard.switch.getOrganizationSwitchPortsBySwitch(ORG_ID, total_pages='all',networkIds=[NET_ID])
print(ports)
#### rest of your codes #####
There are other ways to do this as mentioned in this thread.
devices = dashboard.organizations.getOrganizationDevices(ORG_ID, total_pages='all')
for device in devices:
if device['networkId'] == NET_ID and device['productType'] == 'switch':
ports = dashboard.switch.getDeviceSwitchPorts(device['serial']):
for port in ports:
print(port)

WellyHartanto, Thank you.

Yes, I have about 50 networks that I manage. I want to be able to get the ports in each network so I can say, bounce all ports in a specific vlan. We have "homemade" PoE devices in certain vlans that need bounced every so often if they lose connectivity to a server.

I also want to look to disable unused ports after XX number of days.

I hope Im on the correct path to be able to accomplish those tasks?

Yes it can be done via API.

Take a look at the ouput when you make the API call for switch ports to determine the port VLAN.

For unused port, this thread may help: https://community.meraki.com/t5/Developers-APIs/Find-Unused-Ports-And-Shut-them-Down/m-p/223886/highlight/true#M9740

WellyHartanto,

question about the cycle ports script...

timespan=(31*24*60*60))

Is this 31 days, then 24 hrs in a day, 60 min in an hour, 60 sec in a minute?

so if you wanted a different # of days you just need to change the 31?

the timespan parameter is in seconds and a maximum of 31 days (you may want to read the documentation thoroughly)

What In the
Level 3
Level 3

Looks like "getOrganizationDevices" does not handle stacked switches. When I run it only returns the primary switch ports, and not the member switch ports.

Based on the documentation, I disagree.

devices = dashboard.organizations.getOrganizationDevices(ORG_ID, total_pages='all')
for device in devices:
if device['productType'] == 'switch':
ports = dashboard.switch.getDeviceSwitchPorts(device['serial'])
print(f"{device['name']} has {len(ports)} ports")

HMM. ok. Ill have to take another look. thank you!

I found it. the output splits the stacked switches, and put my 3rd switch which is standalone sandwiched in between.

Thank you again!