Listing Meraki Network Devices Using the API (DevNet)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey network junkies welcome to another devnet video where i'm going to go through and show you how to do something from the objectives of the devnet associate this one is specific to listing network devices in the from the meraki dashboard using the api so i'm going to use an application called postman to use the api and make some requests to see whether or not i could grab this data and then we will take some of that code from postman put that into atom which is a text editor that i like to use for writing python scripts and then we're going to modify that script in order to build a list of network devices from a network that is in a organization and so what i'm going to use is the meraki sandbox the sandbox that's always available to everyone so i highly recommend using the sandbox to learn how to do your your python scripting and learning how to use the api because that's available to everybody so why don't i share my screen and get right into it right here i'm using postman uh postman you can you can go and google this and download it i'm using i'm using a mac so what i have here on the left are collections which i have downloaded or have modified uh you can see i have a bunch of different collections here on the left and i'm going to use that against this meraki sandbox so i i'm not going to show you how to pre-configure this but i want to get into more of the script and how to list network devices using a python script so we can see if we can get this working i already know i need to make get requests right so you have to know what a get request is you have to know what requests are and and how we're going to build this script i already know what i need to get so i need to first see what what organization i'm on and i think i have that somewhere here here organizations listing i i click on that i'm gonna make uh a get request with this url string that is the ap api uh url and uh here is where i'm gonna get my organization so i'll send i should get an output here are my organizations that i have access to and what i'm what i'm really interested in is the sandbox down here so the key thing to this is knowing the organization id because you need to to query then the networks that are in that that organization so now i'm going to click on this get request for networks i want to list all the networks in that organization id and i have that already pre-configured because you see here it's it it wants to want you to put in organization id i'm using the one from this list of organizations and then 236. so if i go back to networks i want to gra get all the networks in this organization so if i hover organization id it's going to use that organization ending in 236 the organization id i'm going to click on send i should get a list of all the networks that are there the first one we see here is this devnet sandbox always on now that's a sandbox that you could use for testing for whatever you need to do so this network id is important because if we want to list all the network devices in this network we need that network id so you've got the organization id you've got the network id and i want to see all the devices in that network on the left here under my collection i have a get request for listing all the devices of a network we're using postman so it's it's just interfacing with the api we're going to send that request and these are all the devices there's not that many but you can see that i am able to get a response see the status is 200 okay i have data and this is using um an api key that was available in the documentation when i was looking at this always on lab so what we want to do is use this a code the code here in python to get this data right so right here in in postman there is a link called code if i click on that uh it's gonna give me the code for python requests and this is a code to get the output that we just saw if you're using different programming languages you can use select another one but i'm i specifically want to use python because that's what's used in the devnet associate exam i'm going to copy this and head over to adam right here and i will paste that code so right here you see that it's already importing a library called requests and we need that in order to run this here where we make a request but before i get to that this is the url the the api i mean the the url that we need to to use in order to query the list of devices to get that list of devices from this network uh and because i have my api key here that's used for the devnet always on lab sandbox uh it's it knows it's that's the network i need right and i have access to that network so i'm going to get those devices so it passes the header which is the meraki api key this is the key and then what we're going to do is make a request using the request library using the we're performing a get request i'm going to pass this url and the headers and the payload and we're gonna whatever we get as a response back from the meraki dashboard we're going to put that into this variable called response and so then we want to print out the response in text so i went ahead and saved this script as getdevices.pi now i'm going to bring over my terminal window i'll zoom in a little bit so you could see this and i want i'm in the directory so i want to run this script so i'm going to do python3 getdevices.pi and we can see what the output is so there are all the network devices in that network but it doesn't look pretty right so the objective of the devnet associate is to get a list of devices you can do it this way and you may get asked a question on the exam about the structure or maybe what is required in order to to get the list of devices it may put out some some code for example and and you have to pick what's missing or or what needs to be added to this in order to get the right response or maybe what uh what kind of uh code uh will provide the output that they give you but this is definitely not a nice way to look at this so what we want to do is take this output here and make it more human readable and this is where i have learned to use json and to take that json output and deserialize it and put it make it into python objects like a list and so the first thing we need to do is import the json library so we'll type import json at the top now here we have our response right so we've printed out this uh what it looks like i'm going to comment it out just to not have it there and instead what we want to do is uh pass the response into the json library and deserialize it so what we can do here is give it another variable so maybe i'll call it uh response json just giving it a name and instead we'll do json dot i believe it's load loads and then we will pass on response text all right so that will hopefully put that into a json format we could uh print out the type i like to do this to see what what type it is data type we should see it as a list so i'll rerun that script and so you can see now it is a list right so if i were to remove this and just do print that response json what do you think we would get so you can see it's in json format right you can see this in the beginning but we have other data in here that we may not need if i want to list out just the network device maybe just the model for example if we look here there is a model and it gives you the model and maybe the serial number because i want to if there's more than one mx65 i want to know the serial number for the mx65 this is where we can now iterate through the data and actually use a for loop so what what i want to do is for uh four devices and or something let's see i don't like the way that reads so what i really want to do is just do uh all devices and then for devices device in all devices will create a for loop and what i want to do is iterate through each each model and just print out the model and also the serial number so i want to do print and let's say model colon and i'll use a format string i'm going to tab it and also say serial number another format string or variable so we'll do format and we're going to call uh device and what we want to get from from here i'm going to call from the device in this list the model number so we need model all lowercase there and we also want the serial number and i believe it was just cereal so if i go back there cereal so hopefully this works right so i think that looks good i'll save it and you can see we've had it like this json data kind of messed something up there i'll clear the scroll back now we'll print and there you go i have now printed out a list of network devices in this network which is this network right here right so this is the network where i'm using a get request to get a list of all the devices but it gave me a lot more data than i really wanted so i really just wanted to go through the data and grab the model number and also the serial and i got here now a model mx61 mx65 a port switch and an app along with their serial numbers so that is how i'm able to go through that data and create a list of devices from the meraki dashboard using the api and just create a list of network devices so i hope that that is useful for you uh how how i was able to use the requests and json library query the api url using the api key to access the data convert the output into a to json or actually deserialize that and then and make it a python list a python object and then in that list iterate through all of that and just pull out the device model and serial number so hopefully that was useful for you and it helps you in your devnet associate studies if you found this video useful give me a like button and subscribe to the channel thanks guys
Info
Channel: Rowell Dionicio
Views: 3,375
Rating: undefined out of 5
Keywords: devnet, cisco, python, netdevops
Id: TDZm8BVoimY
Channel Id: undefined
Length: 14min 29sec (869 seconds)
Published: Wed Nov 11 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.