retrieved list of MRs not complete.

Solved
meerakikat
Conversationalist

retrieved list of MRs not complete.

Hi all

I have a script that, when run, extracts all Access Points (device['model'].startswith('MR')) for all networks in an organization. It also checks if they're online, so I avoid getting the ones not connected.

I do not seem to get all the MR devices - I'm checking on the dashboard and there are MRs that are not on my list. Not sure what's going on - if anybody can take a look and verify it would be great!

Thank you in advance for your time!

m

 

 

 

import requests
import pandas as pd
import getpass

# Prompt the user to enter the Meraki API key
API_KEY = getpass.getpass(prompt='Enter your Meraki API key: ')

# Base URL for the Meraki Dashboard API
BASE_URL = 'https://api.meraki.com/api/v1'

# Headers for API requests
headers = {
    "Authorization": f'Bearer {API_KEY}',
    "Accept": "application/json"
}

# Function to get organization ID
def get_organizations():
    url = f'{BASE_URL}/organizations'
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Function to get device statuses for an organization
def get_device_statuses(org_id):
    url = f'{BASE_URL}/organizations/{org_id}/devices/statuses'
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Function to get networks for an organization
def get_networks(org_id):
    url = f'{BASE_URL}/organizations/{org_id}/networks'
    response = requests.get(url, headers=headers)
    response.raise_for_status()
    return response.json()

# Get the list of organizations
organizations = get_organizations()

# Print the list of organizations and prompt the user to select one
print("Available Organizations:")
for idx, org in enumerate(organizations):
    print(f"{idx}. {org['name']} (ID: {org['id']})")

org_index = int(input("Enter the number of the organization you want to use: "))
org_id = organizations[org_index]['id']

# Get the device statuses for the selected organization
device_statuses = get_device_statuses(org_id)

# Get the list of networks for the selected organization
networks = get_networks(org_id)

# Prepare data for CSV
network_data = []

# Iterate through each network to get device information
for network in networks:
    # Filter for MR Access Points that are connected
    connected_devices = [device for device in device_statuses if device['networkId'] == network['id'] and device['model'].startswith('MR') and device['status'] == 'online']
    
    for device in connected_devices:
        network_data.append({
            'Network Name': network['name'],
            'Device Name': device['name'],
            'Network ID': network['id'],
            'Device Serial': device['serial']
        })

# Create a DataFrame from the data
df = pd.DataFrame(network_data)

# Output the DataFrame to a CSV file
output_file = 'connected_network_devices.csv'
df.to_csv(output_file, index=False)

print(f'Connected MR Access Point information saved to {output_file}')

 

 

 

1 Accepted Solution
Jamieinbox
Building a reputation

Hi, I have some code out on github that I believe does what you need.
Merakicode/APs&Ser_PerNetwork at main · jadexing/Merakicode (github.com)

Merakicode/AssetDataDump at main · jadexing/Merakicode (github.com)


It's been a while since I've run the first, I think it created a spreadsheet (or prints to screen) the second  one creates a spreadsheet of all assets (any Cisco AP, not switches) per network and count.

Hope this helps.

View solution in original post

9 Replies 9
sungod
Kind of a big deal

How many devices in your org?

 

https://developer.cisco.com/meraki/api-v1/get-organization-devices-statuses/ is paged, by default you'll get just one page with the first 1,000 devices.

 

Why not use the Meraki Python library? It'll handle pagination for you, then you can add total_pages="all" to get all the pages.

 

As you only want wireless devices, I'd set productTypes array to "wireless" so that you don't need to filter results on "MR" in the model name.

 

You are also filtering on "online", that excludes devices with "alerting" status, which is effectively "online but there's a problem", again you can filter on a set of statuses when you make the call to avoid returning unwanted devices.

meerakikat
Conversationalist

Hi @sungod 

1. the numbers are definitely NOT on the thousands &

2. i use a corporate laptop and, for some reason, it doesn't like the meraki python library:

 

>>> from meraki import meraki
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'meraki' from 'meraki' (C:\Program Files\Python312\Lib\site-packages\meraki\__init__.py)
>>>

 

John_on_API
Meraki Employee
Meraki Employee

 

from meraki import meraki

 

Instead, use:

 

import meraki

 

 

PhilipDAth
Kind of a big deal
Kind of a big deal

Going sideways, have you looked at getOrganizationInventoryDevices?

https://developer.cisco.com/meraki/api-v1/get-organization-inventory-devices/

 

You can use a call like this to get all the MRs from all networks in a single API call.

 

devices=dashboard.organizations.getOrganizationInventoryDevices(orgId, total_pages='all', productTypes=['wireless'])

 You could also look at the filter usedState.  You will then need to check that the return list of devices is online.

 

 

If you don't mind using an early access API, then check out getOrganizationDevicesStatuses.  It is like above but also returns the device status.

https://developer.cisco.com/meraki/api-v1/api-reference-early-access-api-platform-monitor-devices-st...

 

It has an extra filter called "statuses".

To use this you need to enable the early access API feature.

PhilipDAth_0-1721214658050.png

 

PhilipDAth_1-1721214674866.png

 

GreenMan
Meraki Employee
Meraki Employee

Don't forget too - a lot of newer Meraki APs are CWs; they don't start with MR at all

meerakikat
Conversationalist

The models we have all start with MR. I've checked in Org overview.

Jamieinbox
Building a reputation

This had bit me with my script, but was able to modify with CW, too.

Jamieinbox
Building a reputation

Hi, I have some code out on github that I believe does what you need.
Merakicode/APs&Ser_PerNetwork at main · jadexing/Merakicode (github.com)

Merakicode/AssetDataDump at main · jadexing/Merakicode (github.com)


It's been a while since I've run the first, I think it created a spreadsheet (or prints to screen) the second  one creates a spreadsheet of all assets (any Cisco AP, not switches) per network and count.

Hope this helps.

meerakikat
Conversationalist

i'll have to check using my personal laptop. thank you!

Get notified when there are additional replies to this discussion.