• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Rowell Dionicio

Get Techie With It

  • Home
  • About
  • Resources
    • Archives
    • Book List
    • YouTube
  • Learn
    • DevNet Associate
    • PCNSA Certified
  • Blog
  • Contact
  • Show Search
Hide Search

Listing Meraki Network Devices Using the API (DevNet)

November 11, 2020 By Rowell Leave a Comment

Starting with the basics of network automation involves learning how to construct code. The DevNet Associate will test against your ability to perform this task. 

In this example, I will leverage the Meraki API in order to get a list of devices part of a network within an Organization.

Anyone can get started with this as Meraki provides a DevNet Sandbox for people to test their Python capabilities. 

I’ll be using just three things for this lab:

  • Postman
  • Meraki Sandbox
  • Atom

Postman will help us construct our script with ease. By exploring the API with Postman, we can see which API to use that will give us the results we require. Atom is my text editor of choice.

When writing the script, we’ll eventually need to include a few libraries. Here’s what we’ll be using:

Libraries

  • Requests
  • json

The requests library is exactly how we’re going to make our request to the Meraki API. We want to request a list of devices from a network that is part of an organization.

When we make the request, we’ll get data in return and it will be in json format. We need a way to work with that json data and that’s wha the json library is for.

Meraki has their API documented very well. If our goal is to get a list of devices then we simply need to find which API call needs to be made.

There is a GET request called getNetworkDevices and we’re given the URL needed to make that request. 

From that URL, we need to pass a network ID. To find the network ID we can list all networks in an organization. 

To make a request to the Meraki API, we need to provide an API key. In this example, the API key being used was provided by Meraki from their own examples.

We have a variable defined which will contain the GET response of our request.

I’ll pass the variable into the json library and decode it into a Python object which will be a list in this case. We can tell this is a list if we pass devicesXML into the type function.

Now that we have our data in a Python object, we an iterate through it. This is where we tackle the core objective of listing devices in Meraki. We’ll do this with a for loop.

import requests
import json

url = "https://api.meraki.com/api/v1/networks/L_646829496481105433/devices"

payload = {}
headers = {
     'X-Cisco-Meraki-API-Key': '093b24e85df15a3e66f1fc359f4c48493eaa1b73'
 }

response = requests.request("GET", url, headers=headers, data=payload)

allDevices = json.loads(response.text)

for device in allDevices:
    print("Model: {} \t Serial: {}".format(device["model"], device["serial"]))

The output:

% python3 getDevices.py
Model: MX65      Serial: Q2QN-9J8L-SLPD
Model: MS220-8P      Serial: Q2HP-F5K5-R88R
Model: MR53      Serial: Q2MD-BHHS-5FDL

Share this:

  • Facebook
  • LinkedIn
  • Twitter

Related

Filed Under: Coding, DevNet Associate Tagged With: devnet, python

About Rowell

Wi-Fi expert. Coffee addict ☕️. Tech nerd. Business owner.

Reader Interactions

Leave a Reply Cancel reply

Primary Sidebar

Recent Posts

  • Passed Palo Alto Networks Certified Security Administrator (PCNSA)
  • 5 Years Running
  • Q4 2021 and Yearly Income Report
  • I PASSED JNCIA-MistAI
  • Admins and Role-Based Access Control – PCNSA

Categories

  • bschool
  • Certifications
  • Coding
  • DevNet Associate
  • Events
  • Lab
  • Networking
  • Personal
  • Podcasting
  • Professional
  • Reviews
  • Security
  • Short Stories
  • Uncategorized
  • Wireless

Archives

  • May 2022
  • January 2022
  • December 2021
  • November 2021
  • August 2021
  • July 2021
  • April 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • August 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • November 2018
  • September 2018
  • August 2018

Copyright © 2022 · Written by Rowell Dionicio · You're awesome.

 

Loading Comments...