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}')