A Meraki Python Script Producing Clients Per Network Per 30 Days Using ChatGPT
It took quite a few stumbles down the rabbit whole, but it worked.
Picture created by DALL E 3
There was a script out in the Meraki code on Github that is clientcount.py. I parsed through this code with ChatGPT and I found a couple things:
-The code only pulls devices with, “MR.” This is problematic if you have CW9166’s in your networks.
-There are Meraki Throttlers you need in code so you can make requests and not essentially time out with your data requests.
-There are pagination issues that will limit your answers. A 10-person user count (as I got back many times) is probably not accurate for any access point.
It took time going back and forth with simple tasks to try adding in more features and elements to the code.
I am excited to present this code. I have similar code that tosses this data into an excel spreadsheet.
This blog post medium presented double spaces when copying and pasting code. I am not going to correct this as I don’t want to mess up the Python format.
Here is the code in GitHun.
https://github.com/jadexing/Merakicode
"""
Thanks for checking out my blog and taking this journey with me. If you have some tips on the subject, I’d love to hear them. Find me on X @jamiegprice CWNE #510
Meraki Unique Client Count Script
This Python script interacts with the Cisco Meraki Dashboard API to retrieve the unique client count for each network associated with a specific organization. The script prompts the user to provide the Meraki API key and organization number, and then lists the networks associated with the organization. The user can choose to see the unique client count for a specific network or for all networks. The script uses proper request throttling and pagination to collect all data and displays the unique client count per network. Make sure you have the 'requests' library installed. You can install it using 'pip install requests'.
Author: This was made with chat iterations by Jamie G. Price (Wi-Fi Cool Cats) with ChatGPT
"""
import requests
import time
# Function to retrieve a list of clients from a specific network with pagination
def get_clients(api_key, network_id, timespan):
url = f"https://api.meraki.com/api/v1/networks/{network_id}/clients"
headers = {"X-Cisco-Meraki-API-Key": api_key}
params = {"timespan": timespan, "perPage": 100}
clients_list = []
while True:
response = requests.get(url, headers=headers, params=params)
if response.status_code == 200:
clients_data = response.json()
clients_list.extend(clients_data)
if 'next' in response.links:
url = response.links['next']['url']
time.sleep(0.5) # Throttle the requests to avoid rate-limiting
else:
break
else:
print(f"Error: Unable to fetch clients for Network ID: {network_id}")
break
return clients_list
# Meraki API Key input
api_key = input("Enter your Meraki API key: ")
# Ask for organization number
org_number = input("Enter the organization number: ")
# Retrieve and display the networks associated with the organization
url = f"https://api.meraki.com/api/v1/organizations/{org_number}/networks"
headers = {"X-Cisco-Meraki-API-Key": api_key}
response = requests.get(url, headers=headers)
if response.status_code == 200:
networks_data = response.json()
print("\nNetworks associated with the organization:")
for index, network in enumerate(networks_data, start=1):
print(f"{index}. {network['name']} (Network ID: {network['id']})")
# Ask user to select the network for unique client count
while True:
try:
selected_network = int(input("\nEnter the number of the network you want to see the unique client count for (or 0 for all networks): "))
if 0 <= selected_network <= len(networks_data):
break
else:
print("Invalid input. Please enter a valid number.")
except ValueError:
print("Invalid input. Please enter a valid number.")
# Ask user for the number of days for unique client count
while True:
try:
days = int(input("\nEnter the number of days for unique client count (up to 30 days): "))
if 1 <= days <= 30:
break
else:
print("Invalid input. Please enter a number between 1 and 30.")
except ValueError:
print("Invalid input. Please enter a valid number.")
print("\nPlease wait while gathering data...")
# Retrieve unique client count for the selected network(s)
if selected_network == 0:
for network in networks_data:
network_id = network['id']
network_name = network['name']
clients = get_clients(api_key, network_id, days * 24 * 60 * 60)
unique_clients = set(client['mac'] for client in clients)
print(f"Network '{network_name}': {len(unique_clients)} unique clients")
else:
network_id = networks_data[selected_network - 1]['id']
network_name = networks_data[selected_network - 1]['name']
clients = get_clients(api_key, network_id, days * 24 * 60 * 60)
unique_clients = set(client['mac'] for client in clients)
print(f"Unique clients for Network '{network_name}': {len(unique_clients)}")
else:
print("Unable to fetch networks. Please check your API key and organization number.")