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

Lab Materials

 

 

Exercise A

Install Python 3.6, along with the requests & Meraki Dashboard API modules, and a text editor.

For macOS users:

  1. Install Homebrew first, if not already present, in terminal:
    ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
  2. Install Python 3.6
    brew install pyenv
  3. Install PyEnv
    pyenv install 3.6.5
  4. Install the requests module
    pip3 install --upgrade requests
  5. Install Dashboard API module
    pip3 install --upgrade meraki
  6. To make sure your installation is successful, launch Python
    python3
  7. Now you should see the >>> input, and you can interact with Meraki with a request for the list of organizations
    from meraki import meraki
    meraki.myorgaccess('e24759c28edd1d97715a6ba9ea8bc679c5d2706b')
  8. You should see similar results being returned:
    {'id': 578149602163687854, 'name': 'Public API Lab'}

For Windows users:

  1. Download the latest 3.6 version from https://www.python.org/downloads/
  2. Be sure to select the “Add Python 3.6 to PATH” checkbox, then “install Now”.
  3. Install the requests module, in command prompt (cmd.exe)
    pip install --upgrade requests
  4. Install Dashboard API module
    pip install --upgrade meraki
  5. To make sure your installation is successful, launch Python
    python
  6. Now you should see the >>> input, and you can interact with Meraki with a request for the list of organizations
    from meraki import meraki
    meraki.myorgaccess('e24759c28edd1d97715a6ba9ea8bc679c5d2706b')
  7. You should see similar results being returned:
    {'id': 578149602163687854, 'name': 'Public API Lab'

Text editor:

  1. Download a developer text editor if you don’t already have one. Some options include Sublime, Atom, Visual Studio Code, and Notepad++.
  2. 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

Python Jumpstart

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.

  1. 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:
    1. Organization > Settings > Dashboard API access
  2. Gather the prerequisite data while logged into Dashboard.
    1. API key: Dashboard > upper-right email login > My profile > API access
    2. Organization ID: https://dashboard.meraki.com/api/v0/organizations in same browser session
  3. Open the uplink.py Python file in your editor.
  4. 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.
  5. (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:

  1. 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
  2. Enter API key and org ID as needed.
  3. 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
    meraki.py
  • Login to the Meraki Lab Dashboard organization
    (login with credentials mer[email protected] / 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

  1. 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.
  2. 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

  1. 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)
  2. 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

  1. 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.
  2. You will only claim that one device into the network, no more.

Part 4. Update device properties

  1. What’s your favorite/next vacation spot? Enter it in for the str variable my_address!
  2. 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

  1. 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.
  2. Make sure that the SSID is enabled. Hint: the number for the first SSID is 0.

Bonus: Update multiple SSIDs’ config

  1. With two lines, change the configuration for the other 14 SSIDs as well.
  2. The built-in range function can help here; try calling “help(range)” in the Python interactive interpreter to see its usage.
  3. 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.