Update work/tbx/meraki/sandbox_api.md
This commit is contained in:
@@ -1,109 +1,277 @@
|
||||
# automation-scripts
|
||||
To help you get started with using the Meraki Dashboard API in a sandbox environment, I'll provide you with a project structure and an example script that you can use as a template. This project folder will include a sample Python script, a configuration for environment variables, and instructions for running the script.
|
||||
|
||||
Meraki Dashboard API automation/migration scripts in Python 3
|
||||
|
||||
# Table of contents
|
||||
|
||||
* [Notes](#notes)
|
||||
* [Running scripts locally](#running-scripts-locally)
|
||||
* [Running scripts in the Cisco DevNet Code Exchange development environment](#running-scripts-in-the-cisco-devnet-code-exchange-development-environment)
|
||||
* [Descriptions of scripts in this repository](#descriptions-of-scripts-in-this-repository)
|
||||
|
||||
# Notes
|
||||
|
||||
Here you can find Meraki Dashboard API scripts written for Python 3.
|
||||
|
||||
**NOTE: These scripts will not run in Python 2. Make sure you are using Python 3 with the appropriate commands for your operating system. You can check your Python version with command "python --version" in Windows and "python3 --version" in Linux/Mac.**
|
||||
|
||||
**IMPORTANT NOTE: Some of the older scripts in this repository use the Meraki Dashboard API v0, which is end of life and unsupported. If you are using a script written for v0 and want it converted to v1, please raise an issue about it.**
|
||||
|
||||
Check back from time to time, as new scripts are added and existing ones are sometimes polished and improved after initial posting. Please note that since the Meraki APIs are expanded constantly, there may be more efficient ways to complete a task than what was available when these scripts were created. For the latest info on Meraki APIs, visit: https://developer.cisco.com/meraki/whats-new/
|
||||
|
||||
# Running scripts locally
|
||||
|
||||
To run scripts on your computer locally, you will need to have Python 3 installed, as well as possibly some optional modules, such as the Meraki module, Requests or PyYAML.
|
||||
* For the latest version of Python 3, please visit: https://www.python.org/downloads/
|
||||
* The easiest way to install optional packages is via pip: https://pypi.org/project/pip/
|
||||
* Although not a requirement, many developers use Python virtual environements to run their scripts: https://docs.python.org/3/library/venv.html
|
||||
|
||||
The opening comments of the scripts contained in this repository will typically include an explanation of the correct syntax to run the script, as well as any required third party modules.
|
||||
|
||||
Many scripts support passing your Meraki Dashboard API key via an OS environment variable. The variable name used is `MERAKI_DASHBOARD_API_KEY`. Please refer to documentation of your operating system to configure this. Most scripts provide an alternate way to provide the key as well, such as a config file or a command line argument, in case you prefer not to modify your environment variables.
|
||||
|
||||
# Running scripts in the Cisco DevNet Code Exchange development environment
|
||||
|
||||
If you run this project using the [Cisco Exchange Dev environment](https://developer.cisco.com/codeexchange/github/repo/meraki/automation-scripts/)
|
||||
|
||||

|
||||
|
||||
For some scripts, you can add `Meraki API key` as a parameter.
|
||||
### Project Folder Structure
|
||||
|
||||
```
|
||||
python3 tag_all_ports.py -k <api_key> -t <tag> [-o <org_name>]
|
||||
[-n <network_name>] [-f <filter>] [-a <add/remove>]
|
||||
automation-scripts/
|
||||
├── README.md
|
||||
├── scripts/
|
||||
│ ├── clientcount.py
|
||||
│ ├── get_license.py
|
||||
│ └── inventorycsv.py
|
||||
├── requirements.txt
|
||||
└── setup_env.sh
|
||||
```
|
||||
|
||||
For others set as an environment variable named `MERAKI_DASHBOARD_API_KEY`, `DASHBOARD_API_ORG_ID`, `DASHBOARD_API_SHARD_ID`
|
||||
### README.md
|
||||
|
||||
For example
|
||||
```
|
||||
export DASHBOARD_API_KEY=d03190ff333a3c7feaed89fec5b3b2529f59e8ec
|
||||
```markdown
|
||||
# Meraki Dashboard API Automation Scripts
|
||||
|
||||
This project contains Python scripts for automating tasks with the Meraki Dashboard API.
|
||||
|
||||
## Prerequisites
|
||||
|
||||
- Python 3.x
|
||||
- Meraki API Key
|
||||
- Required Python packages (listed in `requirements.txt`)
|
||||
|
||||
## Setup
|
||||
|
||||
1. Clone this repository.
|
||||
2. Install the required Python packages:
|
||||
|
||||
```sh
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. Set up your environment variables by sourcing the `setup_env.sh` file:
|
||||
|
||||
```sh
|
||||
source setup_env.sh
|
||||
```
|
||||
|
||||
## Running Scripts
|
||||
|
||||
### Get Client Count
|
||||
|
||||
```sh
|
||||
python scripts/clientcount.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab"
|
||||
```
|
||||
|
||||
You can test these scripts using [Cisco Meraki Always-on sandbox](https://devnetsandbox.cisco.com/RM/Diagram/Index/a9487767-deef-4855-b3e3-880e7f39eadc?diagramType=Topology) with `MERAKI_DASHBOARD_API_KEY`
|
||||
### Get License Information
|
||||
|
||||
In the Cisco Exchange Dev environment, you can try with the following commands:
|
||||
|
||||
Install Python packages
|
||||
|
||||
```
|
||||
pip install requests pyyaml pymongo pysnmp meraki
|
||||
```sh
|
||||
python scripts/get_license.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab"
|
||||
```
|
||||
|
||||
Run `clientcount.py`
|
||||
### Get Inventory List
|
||||
|
||||
```
|
||||
python clientcount.py -k d03190ff333a3c7feaed89fec5b3b2529f59e8ec -o "DeLab"
|
||||
```sh
|
||||
python scripts/inventorycsv.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab" -f DeLab_inventory_list.csv
|
||||
```
|
||||
|
||||
Terminal output (sample)
|
||||
## Notes
|
||||
|
||||
```
|
||||
Total unique client MAC addresses across all WLAN APs: 38
|
||||
Make sure to check each script's usage information in the header of the script.
|
||||
```
|
||||
|
||||
Get the license info for Meraki organization(s)
|
||||
```
|
||||
python get_license.py -k d03190ff333a3c7feaed89fec5b3b2529f59e8ec -o "DeLab"
|
||||
### requirements.txt
|
||||
|
||||
```txt
|
||||
requests
|
||||
pyyaml
|
||||
pymongo
|
||||
pysnmp
|
||||
meraki
|
||||
```
|
||||
|
||||
Terminal output (sample)
|
||||
```
|
||||
License info for organization "DeLab" (ID: 681155)
|
||||
### setup_env.sh
|
||||
|
||||
Status: OK
|
||||
Expiration date: Oct 13, 2024 UTC
|
||||
```sh
|
||||
#!/bin/bash
|
||||
|
||||
Licensed device counts:
|
||||
wireless 40
|
||||
MS220-8P 9
|
||||
MX65 6
|
||||
MC 4
|
||||
MV 9
|
||||
MS220-8 1
|
||||
SM 5
|
||||
MX250 1
|
||||
MS250-48FP 1
|
||||
# Set your Meraki API key here
|
||||
export MERAKI_DASHBOARD_API_KEY="d03190ff333a3c7feaed89fec5b3b2529f59e8ec"
|
||||
```
|
||||
|
||||
Get an inventory list for a specific organization or all organizations accessible by an administrator to a CSV file.
|
||||
### scripts/clientcount.py
|
||||
|
||||
```
|
||||
python inventorycsv.py -k d03190ff333a3c7feaed89fec5b3b2529f59e8ec -o "DeLab" -f DeLab_inventory_list.csv
|
||||
```python
|
||||
import requests
|
||||
import argparse
|
||||
|
||||
def get_client_count(api_key, org_name):
|
||||
base_url = 'https://api.meraki.com/api/v1'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {api_key}'
|
||||
}
|
||||
|
||||
# Get the list of organizations
|
||||
orgs_response = requests.get(f'{base_url}/organizations', headers=headers)
|
||||
orgs = orgs_response.json()
|
||||
|
||||
# Find the organization ID for the given name
|
||||
org_id = None
|
||||
for org in orgs:
|
||||
if org['name'] == org_name:
|
||||
org_id = org['id']
|
||||
break
|
||||
|
||||
if org_id is None:
|
||||
print(f'Organization {org_name} not found')
|
||||
return
|
||||
|
||||
# Get the list of networks in the organization
|
||||
networks_response = requests.get(f'{base_url}/organizations/{org_id}/networks', headers=headers)
|
||||
networks = networks_response.json()
|
||||
|
||||
# Count the number of unique clients across all networks
|
||||
unique_clients = set()
|
||||
for network in networks:
|
||||
network_id = network['id']
|
||||
clients_response = requests.get(f'{base_url}/networks/{network_id}/clients', headers=headers)
|
||||
clients = clients_response.json()
|
||||
for client in clients:
|
||||
unique_clients.add(client['mac'])
|
||||
|
||||
print(f'Total unique client MAC addresses across all WLAN APs: {len(unique_clients)}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Get client count for a Meraki organization')
|
||||
parser.add_argument('-k', '--api_key', required=True, help='Meraki API key')
|
||||
parser.add_argument('-o', '--org_name', required=True, help='Meraki organization name')
|
||||
args = parser.parse_args()
|
||||
get_client_count(args.api_key, args.org_name)
|
||||
```
|
||||
|
||||
Find file in `Explorer > SRC`
|
||||
### scripts/get_license.py
|
||||
|
||||
In the header of each script, you can find Usage information.
|
||||
```python
|
||||
import requests
|
||||
import argparse
|
||||
|
||||
See also: [Meraki Enterprise Sandbox](https://devnetsandbox.cisco.com/RM/Diagram/Index/e7b3932b-0d47-408e-946e-c23a0c031bda?diagramType=Topology), [Meraki Small Business Sandbox](https://devnetsandbox.cisco.com/RM/Diagram/Index/aa48e6e2-3e59-4b87-bfe5-7833c45f8db8?diagramType=Topology)
|
||||
def get_license_info(api_key, org_name):
|
||||
base_url = 'https://api.meraki.com/api/v1'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {api_key}'
|
||||
}
|
||||
|
||||
# Get the list of organizations
|
||||
orgs_response = requests.get(f'{base_url}/organizations', headers=headers)
|
||||
orgs = orgs_response.json()
|
||||
|
||||
# Find the organization ID for the given name
|
||||
org_id = None
|
||||
for org in orgs:
|
||||
if org['name'] == org_name:
|
||||
org_id = org['id']
|
||||
break
|
||||
|
||||
if org_id is None:
|
||||
print(f'Organization {org_name} not found')
|
||||
return
|
||||
|
||||
# Get the license information for the organization
|
||||
license_response = requests.get(f'{base_url}/organizations/{org_id}/licenses', headers=headers)
|
||||
license_info = license_response.json()
|
||||
|
||||
print(f'License info for organization "{org_name}" (ID: {org_id})')
|
||||
print(f'Status: {license_info["status"]}')
|
||||
print(f'Expiration date: {license_info["expirationDate"]}')
|
||||
print('Licensed device counts:')
|
||||
for license in license_info['licenses']:
|
||||
print(f'{license["productType"]}:\t{license["total"]}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Get license info for a Meraki organization')
|
||||
parser.add_argument('-k', '--api_key', required=True, help='Meraki API key')
|
||||
parser.add_argument('-o', '--org_name', required=True, help='Meraki organization name')
|
||||
args = parser.parse_args()
|
||||
get_license_info(args.api_key, args.org_name)
|
||||
```
|
||||
|
||||
### scripts/inventorycsv.py
|
||||
|
||||
```python
|
||||
import requests
|
||||
import csv
|
||||
import argparse
|
||||
|
||||
def get_inventory(api_key, org_name, filename):
|
||||
base_url = 'https://api.meraki.com/api/v1'
|
||||
headers = {
|
||||
'Authorization': f'Bearer {api_key}'
|
||||
}
|
||||
|
||||
# Get the list of organizations
|
||||
orgs_response = requests.get(f'{base_url}/organizations', headers=headers)
|
||||
orgs = orgs_response.json()
|
||||
|
||||
# Find the organization ID for the given name
|
||||
org_id = None
|
||||
for org in orgs:
|
||||
if org['name'] == org_name:
|
||||
org_id = org['id']
|
||||
break
|
||||
|
||||
if org_id is None:
|
||||
print(f'Organization {org_name} not found')
|
||||
return
|
||||
|
||||
# Get the inventory for the organization
|
||||
inventory_response = requests.get(f'{base_url}/organizations/{org_id}/inventory/devices', headers=headers)
|
||||
inventory = inventory_response.json()
|
||||
|
||||
# Write the inventory to a CSV file
|
||||
with open(filename, 'w', newline='') as csvfile:
|
||||
fieldnames = ['serial', 'model', 'networkId', 'mac', 'claimedAt']
|
||||
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
|
||||
|
||||
writer.writeheader()
|
||||
for device in inventory:
|
||||
writer.writerow(device)
|
||||
|
||||
print(f'Inventory for organization "{org_name}" saved to {filename}')
|
||||
|
||||
if __name__ == '__main__':
|
||||
parser = argparse.ArgumentParser(description='Get inventory for a Meraki organization')
|
||||
parser.add_argument('-k', '--api_key', required=True, help='Meraki API key')
|
||||
parser.add_argument('-o', '--org_name', required=True, help='Meraki organization name')
|
||||
parser.add_argument('-f', '--filename', required=True, help='Output CSV file')
|
||||
args = parser.parse_args()
|
||||
get_inventory(args.api_key, args.org_name, args.filename)
|
||||
```
|
||||
|
||||
### Instructions for Running the Project
|
||||
|
||||
1. **Clone the repository:**
|
||||
|
||||
```sh
|
||||
git clone https://github.com/yourusername/automation-scripts.git
|
||||
cd automation-scripts
|
||||
```
|
||||
|
||||
2. **Install required Python packages:**
|
||||
|
||||
```sh
|
||||
pip install -r requirements.txt
|
||||
```
|
||||
|
||||
3. **Set up environment variables:**
|
||||
|
||||
```sh
|
||||
source setup_env.sh
|
||||
```
|
||||
|
||||
4. **Run the scripts:**
|
||||
|
||||
- **Get client count:**
|
||||
|
||||
```sh
|
||||
python scripts/clientcount.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab"
|
||||
```
|
||||
|
||||
- **Get license information:**
|
||||
|
||||
```sh
|
||||
python scripts/get_license.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab"
|
||||
```
|
||||
|
||||
- **Get inventory list:**
|
||||
|
||||
```sh
|
||||
python scripts/inventorycsv.py -k $MERAKI_DASHBOARD_API_KEY -o "DeLab" -f DeLab_inventory_list.csv
|
||||
```
|
||||
|
||||
This setup should give you a good starting point for working with the Meraki Dashboard API in a sandbox environment. If you have any further questions or need additional examples, feel free to ask!
|
||||
Reference in New Issue
Block a user