Automation with Dashboard API
Automation with Python – API Lab
Getting Started
The following lab guide will help get you familiar with using the Dashboard API and the Python programming language. It consists of a few exercises to get you up and running quickly. We will first start with getting the prerequisites installed and configured. Then we will modify a pre-written Python script to interact with the API. You only need a basic understanding of programming and Meraki to complete this lab.
Download the Lab Materials
Exercise A
Install Python 3.6, along with the requests & Meraki Dashboard API modules, and a text editor.
For macOS users:
- Install Homebrew first, if not already present, in terminal:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
- Install Python 3.6
brew install pyenv
- Install PyEnv
pyenv install 3.6.5
- Install the requests module
pip3 install --upgrade requests
- Install Dashboard API module
pip3 install --upgrade meraki
- To make sure your installation is successful, launch Python
python3
- Now you should see the
>>>
input, and you can interact with Meraki with a request for the list of organizationsfrom meraki import meraki meraki.myorgaccess('e24759c28edd1d97715a6ba9ea8bc679c5d2706b')
- You should see similar results being returned:
{'id': 578149602163687854, 'name': 'Public API Lab'}
For Windows users:
- Download the latest 3.6 version from https://www.python.org/downloads/
- Be sure to select the “Add Python 3.6 to PATH” checkbox, then “install Now”.
- Install the requests module, in command prompt (cmd.exe)
pip install --upgrade requests
- Install Dashboard API module
pip install --upgrade meraki
- To make sure your installation is successful, launch Python
python
- Now you should see the
>>>
input, and you can interact with Meraki with a request for the list of organizationsfrom meraki import meraki meraki.myorgaccess('e24759c28edd1d97715a6ba9ea8bc679c5d2706b')
- You should see similar results being returned:
{'id': 578149602163687854, 'name': 'Public API Lab'
Text editor:
- Download a developer text editor if you don’t already have one. Some options include Sublime, Atom, Visual Studio Code, and Notepad++.
- Native TextEdit on macOS or Notepad/WordPad on Windows will not suffice! In addition, watch out for these editors altering certain characters like quotes to non-ASCII encoding, which will cause problems.
Python Jumpstart:
Slide deck on basics of Python, helpful as an introduction to the Python programming language
Exercise B
This is a simple exercise in which you run a pre-written Python script that gathers the uplink status for all devices in your personal org. The script outputs the information to two CSV files, one for appliances, and another for all other devices.
- Log onto Dashboard and check that the Organization that you will use in the lab has been enabled for API access. You will find the setting under:
- Organization > Settings > Dashboard API access
- Gather the prerequisite data while logged into Dashboard.
- API key: Dashboard > upper-right email login > My profile > API access
- Organization ID: https://dashboard.meraki.com/api/v0/organizations in same browser session
- Open the uplink.py Python file in your editor.
- Scan through the file, looking at the comments (lines beginning with # sign) heading each section, to get a rough sense of the script’s workflow.
- (Optional) Store your API key and org ID in a separate login.py file in the same folder.
Example of what the login.py should contain:
-
api_key = '093b24e85df15a3e66f1fc359f4c48493eaa1b73' org_id = '537758'
Run the script in terminal/command, depending on your installation:(for macOS)
python3 uplink.py
(for Windows)python uplink.py
- Enter API key and org ID as needed.
- Examine the two CSV files generated either in text editor or Excel!
Exercise C
This exercise is intended to get your feet wet with using the Dashboard API Python module with a mostly pre-written script that you will need to edit, with some blanks to fill!
The exercise walks you through building a script that 1) creates your network in the org, 2) queries the org’s overall inventory, 3) adds a device from the inventory to your network, 4) updates the location of that device, and finally, 5) updates the network’s SSID config.
You will only need to edit the sections (12 lines total) of the script between the “START EDITING” and “END EDITING” comment blocks. Do NOT change anything outside of those sections.
To get started:
- Download and open lab_public.py in a text editor
- Open terminal/command prompt to the directory of that file
- In a browser, open the Dashboard API module meraki.py file on GitHub
Source code - Login to the Meraki Lab Dashboard organization
(login with credentials meraki.api.lab+ro@gmail.com / meraki123)
You will go back and forth between editing the lab_public.py script and running it, looking up the module’s source code for specific functions, and verifying your progress in the Dashboard org. However, you should NOT be making any changes in the Dashboard GUI directly, as all configuration should be done through API calls.
python3 lab_public.py
or
python lab_public.py
Part 1. Create a network
- Let’s start off easy! For this first part, all you need to do is fill in the variable section with your name, some tags to apply to the network, and the timezone preferred.
- Note that this part calls two functions: getnetworklist with the API key and org ID, along with addnetwork in the module (search for “# Create a network”). The latter function passes in the same two parameters as the former, along with your name as the network name, str (that’s the Python string type) ‘wireless’ for an MR network, the tags, and timezone.
Part 2. Return the inventory
- Here, you’ll need to figure out how to call the getorginventory function. Refer to the function definition in the module, or you can also run “help(meraki.getorginventory)” in the Python interactive interpreter (after importing first with “from meraki import meraki”).
In terminal/command prompt, run “python3”; this starts a Python prompt, then:from meraki import meraki help(meraki.getorginventory)
- The list comprehension then assigns all devices with no network ID (those not yet already in a network) to the variable unused.
Part 3. Claim device into network
- This part of the script randomly selects one of the unused APs from inventory, and now you will need to call another function to claim the device into your wireless network.
- You will only claim that one device into the network, no more.
Part 4. Update device properties
- What’s your favorite/next vacation spot? Enter it in for the str variable my_address!
- Now update your device’s name to be the same as the network name, tags same as network tags, and location to your vacation address, while moving the map maker.
Hint: you do not have to worry about entering latitude/longitude coordinates. Double-check the module’s function (search for “# Update the attributes of a device”) for the exact parameters needed, along with the parameters as specified by the Dashboard API dAutomation with Python – API Labocs.
Part 5. Update first SSID config
- To configure a WPA2-Personal SSID into the first SSID slot, input a different name (than earlier) for your SSID and a PSK with at least eight characters.
- Make sure that the SSID is enabled. Hint: the number for the first SSID is 0.
Bonus: Update multiple SSIDs’ config
- With two lines, change the configuration for the other 14 SSIDs as well.
- The built-in range function can help here; try calling “help(range)” in the Python interactive interpreter to see its usage.
- The purpose of this last part is to practice using a for loop within Python, along with string concatenation and type conversion (changing a data variable from one type to another). Why would these methods resolve the “Bad Request” return with a status of 400?
Success
Congratulations! You’re now on your way automating the Meraki Dashboard API with Python!
Refer to the lab_answers.py file for a working solution and check your work.