Python Script for getting All in One.

Solved
Vmadathil
Here to help

Python Script for getting All in One.

Hi ,

 

I have a requirement for fetching the details of all the Organizations, Under the organization all the Network details, Under Network all the SSID details, Under SSID all the Radius server details as a single output file from Meraki Dashboard. This will help me to understand which Radius server is configured in which SSID -Network-Organization.

 

Could you please help me to find the script.

1 Accepted Solution
Vmadathil
Here to help

I got the relevant information using the following script. Thank you so much for your valuable support.
 
 
import json
import requests
 
# Function to fetch organizations
def get_organizations(api_key):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Function to fetch networks for a specific organization
def get_networks(api_key, organization_id):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Function to fetch SSIDs for a specific network
def get_ssids(api_key, network_id):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Main function to fetch everything
def fetch_all(api_key, organization_id):
    organizations = get_organizations(api_key)
    networks = get_networks(api_key, organization_id)
 
    output_data = []  # List to store output data
 
    for network in networks:
        ssids = get_ssids(api_key, network['id'])
        output_data.append({
            "Organization": organizations[0]['name'],
            "Network": network['name'],
            "SSIDs": ssids
        })
 
    return output_data
 
# Set your Meraki API key and organization ID
api_key = "Meraki-API-Key"
organization_id = "ORG-ID"
 
# Call the main function to fetch everything
output_data = fetch_all(api_key, organization_id)
 
# Write output data to JSON file
output_file_path = "output.json"
with open(output_file_path, "w") as json_file:
    json.dump(output_data, json_file, indent=4)
 
print("Output saved to", output_file_path)

View solution in original post

7 Replies 7
alemabrahao
Kind of a big deal
Kind of a big deal

You will need a set of APIs to be able to do what you need.
 
Here is an example script in Python (I haven't tested it).

 

import os
import csv
from meraki import meraki
 
# Initialize the client
apikey = os.getenv('MERAKI_DASHBOARD_API_KEY')
dashboard = meraki.DashboardAPI(apikey)
 
# Fetch the organizations
orgs = dashboard.organizations.getOrganizations()
 
# Prepare the CSV file
with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerow(["Organization", "Network", "SSID", "Radius Server"])
 
    # Loop through each organization
    for org in orgs:
        org_id = org['id']
        org_name = org['name']
 
        # Fetch the networks
        networks = dashboard.organizations.getOrganizationNetworks(org_id)
 
        # Loop through each network
        for network in networks:
            net_id = network['id']
            net_name = network['name']
 
            # Fetch the SSIDs
            ssids = dashboard.wireless.getNetworkWirelessSsids(net_id)
 
            # Loop through each SSID
            for ssid in ssids:
                ssid_name = ssid['name']
 
                # Fetch the Radius servers
                radius_servers = ssid.get('radiusServers', [])
 
                # Loop through each Radius server
                for server in radius_servers:
                    server_host = server['host']
 
                    # Write the details to the CSV file
                    writer.writerow([org_name, net_name, ssid_name, server_host])

 

If you are not familiar with Python, I advise you to look for an expert to help you.

I am not a Cisco Meraki 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.

Great effort @alemabrahao .

Vmadathil
Here to help

Thank you so much for your prompt reply. 

 

As per my understanding I need to manually apply the following variables only. Rest of the variables will be fetched from the output of the relevant requests. Please confirm.

 

apikey = os.getenv('MERAKI_DASHBOARD_API_KEY')
dashboard = meraki.DashboardAPI(apikey)

Just this one.

 

apikey = os.getenv('MERAKI_DASHBOARD_API_KEY')

I am not a Cisco Meraki 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.

Just a headsup,

apikey = os.getenv('MERAKI_DASHBOARD_API_KEY')

assumes that you have a System Environment Variable created called MERAKI_DASHBOARD_API_KEY, which contains your Meraki API Key.

If you don't have such an environment variable configured, you may set apiKey directly, by replacing the former with

apiKey = "your-api-key"
dashboard = meraki.DashboardAPI(apiKey)

or enter it directly in the dashboard instantiation,

dashboard = meraki.DashboardAPI("your-api-key")
LinkedIn ::: https://blog.rhbirkelund.dk/

Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂

All code examples are provided as is. Responsibility for Code execution lies solely your own.

Additionally, the "apikey" assignment in the example below is is redundant,

apikey = os.getenv('MERAKI_DASHBOARD_API_KEY')
dashboard = meraki.DashboardAPI(apikey)

 

If you specify "dashboard = meraki.DashboardAPI()" without any parameters, it will automatically search your system for the MERAKI_DASHBOARD_API_KEY environment variable, and pull it from there.

Specifying the key allows to to distinguish between different API keys, so forth you have so.

LinkedIn ::: https://blog.rhbirkelund.dk/

Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂

All code examples are provided as is. Responsibility for Code execution lies solely your own.
Vmadathil
Here to help

I got the relevant information using the following script. Thank you so much for your valuable support.
 
 
import json
import requests
 
# Function to fetch organizations
def get_organizations(api_key):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Function to fetch networks for a specific organization
def get_networks(api_key, organization_id):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Function to fetch SSIDs for a specific network
def get_ssids(api_key, network_id):
    headers = {
        "X-Cisco-Meraki-API-Key": api_key,
        "Content-Type": "application/json"
    }
    response = requests.get(url, headers=headers)
    return response.json()
 
# Main function to fetch everything
def fetch_all(api_key, organization_id):
    organizations = get_organizations(api_key)
    networks = get_networks(api_key, organization_id)
 
    output_data = []  # List to store output data
 
    for network in networks:
        ssids = get_ssids(api_key, network['id'])
        output_data.append({
            "Organization": organizations[0]['name'],
            "Network": network['name'],
            "SSIDs": ssids
        })
 
    return output_data
 
# Set your Meraki API key and organization ID
api_key = "Meraki-API-Key"
organization_id = "ORG-ID"
 
# Call the main function to fetch everything
output_data = fetch_all(api_key, organization_id)
 
# Write output data to JSON file
output_file_path = "output.json"
with open(output_file_path, "w") as json_file:
    json.dump(output_data, json_file, indent=4)
 
print("Output saved to", output_file_path)
Get notified when there are additional replies to this discussion.