Giter Club home page Giter Club logo

dashboard-api-python's Introduction

Meraki Dashboard API Python Library

The Meraki Dashboard API Python library provides all current Meraki Dashboard API calls to interface with the Cisco Meraki cloud-managed platform. The library is supported on Python 3.6 or above, and you can install it via PyPI:

pip install meraki

Features

While you can make direct HTTP requests to dashboard API in any programming language or REST API client, using a client library can make it easier for you to focus on your specific use case, without the overhead of having to write functions to handle the dashboard API calls. The Python library can also take care of error handling, logging, retries, and other convenient processes and options for you automatically.

  • Support for all API endpoints, as it uses the OpenAPI specification to generate source code
  • Log all API requests made to a local file as well as on-screen console
  • Automatic retries upon 429 rate limit errors, using the Retry-After field within response headers
  • Get all (or a specified number of) pages of data with built-in pagination control
  • Tweak settings such as maximum retries, certificate path, suppress logging, and other options
  • Simulate POST/PUT/DELETE calls to preview first, so that network configuration does not get changed
  • Includes the legacy module's (version 0.34 and prior) functions for backward compatibility

Setup

  1. Enable API access in your Meraki dashboard organization and obtain an API key (instructions)

  2. Keep your API key safe and secure, as it is similar to a password for your dashboard. If publishing your Python code to a wider audience, please research secure handling of API keys.

  3. Install the latest version of Python 3

  4. Use pip (or an alternative such as easy_install) to install the library:

    • pip install meraki
    • If you have both Python3 and Python2 installed, you may need to use pip3 install meraki
    • If meraki was previously installed, you can upgrade with pip install --upgrade meraki or pip3 install --upgrade meraki
    • You can specify the version of the library, for example pip install meraki==0.100.2 for v0 or pip install meraki==1.0.0b1 for v1

Usage

  1. Export your API key as an environment variable, for example:

    export MERAKI_DASHBOARD_API_KEY=093b24e85df15a3e66f1fc359f4c48493eaa1b73
  2. Alternatively, define your API key as a variable in your source code; this method is not recommended due to its inherent insecurity.

  3. Single line of code to import and use the library goes at the top of your script:

    import meraki
  4. Instantiate the client (API consumer class), optionally specifying any of the parameters available to set:

    dashboard = meraki.DashboardAPI()
  5. Make dashboard API calls in your source code, using the format client.scope.operation, where client is the name you defined in the previous step (dashboard above), scope is the corresponding scope that represents the first tag from the OpenAPI spec, and operation is the operation of the API endpoint. For example, to make a call to get the list of organizations accessible by the API key defined in step 1, use this function call:

    my_orgs = dashboard.organizations.getOrganizations()
  6. If you were using this module versions 0.34 and prior, that file's functions are included in the legacy.py file, and you can adapt your existing scripts by replacing their from meraki import meraki line to import meraki

Examples

You can find fully working example scripts in the examples folder.

Script Purpose
org_wide_clients.py That code collects the clients of all networks, in all orgs to which the key has access. No changes are made, since only GET endpoints are called, and the data is written to local CSV output files.

AsyncIO

asyncio is a library to write concurrent code using the async/await syntax. Special thanks to Heimo Stieg (@coreGreenberet) who has ported the API to asyncio.

The usage is similiar to the sequential version above. However it has has some differences.

  1. Export your API key as an environment variable, for example:

    export MERAKI_DASHBOARD_API_KEY=093b24e85df15a3e66f1fc359f4c48493eaa1b73
  2. Alternatively, define your API key as a variable in your source code; this method is not recommended due to its inherent insecurity.

  3. Single line of code to import and use the library goes at the top of your script:

    import meraki.aio
  4. Instantiate the client (API consumer class), optionally specifying any of the parameters available to set:

    async with meraki.aio.AsyncDashboardAPI() as aiomeraki:

    The async with statement is important here to make sure, that the client sessions will be closed after using the api.

  5. Make dashboard API calls in your source code, using the format await client.section.operation, where client is the name you defined in the previous step (aiomeraki above), section is the corresponding group (or tag from the OpenAPI spec) from the API docs, and operation is the name (or operation ID from OpenAPI) of the API endpoint. For example, to make a call to get the list of organizations accessible by the API key defined in step 1, use this function call:

    my_orgs = await aiomeraki.organizations.getOrganizations()
  6. Run everything inside an event loop.

import asyncio

if __name__ == "__main__":
    loop = asyncio.get_event_loop()
    loop.run_until_complete(my_async_entry_point())
    
    # if you are using Python 3.7+ you can also simply 
    # use the following line instead of the two lines above
    asyncio.run(my_async_entry_point())

Examples

You can find fully working example scripts in the examples folder.

Script Purpose
aio_org_wide_clients.py That code is a asyncio port from org_wide_clients.py and collects the clients of all networks, in all orgs to which the key has access. No changes are made, since only GET endpoints are called, and the data is written to local CSV output files.
aio_ips2firewall.py That code will collect the source IP of security events and creates L7 firewall rules to block them. usage: aio_ips2firewall.py [-h] -o ORGANIZATIONS [ORGANIZATIONS ...] [-f FILTER] [-s] [-d DAYS]

dashboard-api-python's People

Contributors

coregreenberet avatar danmanners avatar grutz avatar jleatham avatar longbeachhxc avatar nathanwiens avatar nerdguru avatar pocc avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.