python script multiple organizations and networks ( looking for help)

AlbertoFeoli
Here to help

python script multiple organizations and networks ( looking for help)

Hi Gents,

 

 

Im starting in the python world. I recently wrote a code thanks to the apis and data available.

 

The code I wrote is to get the organizations my company owns but im also adding things like printing the networks of each organization.

 

Code is:

import meraki, requests, time, apikey, json


#Organizations
print('********************************************************************')
print('Listing Organizations xxxx')
print('********************************************************************')
url = "https://api.meraki.com/api/v1/organizations"
payload={}
headers = { 'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
allOrg = json.loads(response.text)
for Organization in allOrg:
print("Name: {} \t ID: {}".format(Organization["name"],Organization["id"]))

print('********************************************************************')
print('Listing Networks each Organization')
print('********************************************************************')
print('EU Networks')

url = "https://api.meraki.com/api/v1/organizations/535660/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))

print('********************************************************************')
print('US Networks')

url = "https://api.meraki.com/api/v1/organizations/419633/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))

print('********************************************************************')
print('APAC Networks')

url = "https://api.meraki.com/api/v1/organizations/671036344478206491/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))
print('********************************************************************')


How I can build a loop to run the code but with one single loop instead of repeating the same code for each organization?

 

This code works just fine, but im sure there must a be a way for me to simplify it and make it shorter in terms of the code

 

Thanks in advance .

9 Replies 9
Eddysanoli
Getting noticed

Hey! You could use the syntax provided by the meraki package to not manually structure a request each time:

 

This block of code:

 

url = "https://api.meraki.com/api/v1/organizations/535660/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)

 

 

Can be simplified to:

 

dashboard = meraki.DashboardAPI(API_KEY)
networks = dashboard.organizations.getOrganizationNetworks("535660")

 

 

Another cool thing is that, that "dashboard" variable can be shared between all the different calls that you want to make, so you can place a single `dashboard = meraki.DashboardAPI` statement at the top and then just use it for all the other API calls that you make

 

You can see this link to see how to use the python method (When you access the page you will have a section to your right with parameters. Click on "templates" and then "meraki python library" to see how to use that specific method using the meraki library). 

 

Hi Eddysanoli,

 

 

Thanks for replying. But not sure what I have done wrong that it does not work as you suggested. Let me be a bit more clear  🙂 . We have three organizations and each organization a bunch of networks. What code I can use to loop into each organization and get the networks of each organization?

 

 

 

Sorry! I just gave an example of how to replace one of your blocks of code with the other. Let me give you an example of how you can do what you described:

 

from typing import List

# Create a dashboard connection
dashboard = meraki.DashboardAPI(apikey.api_key)

# Get all the organizations
organizations : List[dict] = dashboard.organizations.getOrganizations()

# Go through each organization
for organization in organizations:

   # Get all networks under the current organization
   networks : List[dict] = dashboard.organizations.getOrganizationNetworks(
     organization["id"], total_pages='all'
   )

 

Added type annotations in some variables (The ": List[dict]" bit) to make it a bit more clear what data type are you receiving.

Thanks again, but still not understanding the part of networks for each organization and apologies : This is what ive done so far (just learning python as I mentioned above):

 

 

import meraki, requests, time, apikey, json


#Organizations
print('********************************************************************')
print('Listing Organizations xxxx')
print('********************************************************************')
url = "https://api.meraki.com/api/v1/organizations"
payload={}
headers = { 'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
allOrg = json.loads(response.text)
for Organization in allOrg:
print("Name: {} \t ID: {}".format(Organization["name"],Organization["id"]))


#OrganizationNetworks:


networks : dashboard.organizations.getOrganizationNetworks(
organization["id"], total_pages='all'
)

 Not sure what im missing

No worries! You can paste the code above and it should work. The code above is functional given that you have installed the meraki package

no, it does not work.  is not printing any network at all . Alright thanks so much for your time and help Eddy.

Oh. Okay. Sorry. You can make sure that the line

organizations : List[dict] = dashboard.organizations.getOrganizations()
print(organizations)

 

Works by printing the organizations variable. Hopefully you get something out

No Eddy, still not printing the networks for each organization. I think I will still using the old method of repeating the calls for each organization and get the networks from each organization the way I did it initially:

 

import meraki, requests, time, apikey, json


#Organizations
print('********************************************************************')
print('Listing Organizations xxxx')
print('********************************************************************')
url = "https://api.meraki.com/api/v1/organizations"
payload={}
headers = { 'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
allOrg = json.loads(response.text)
for Organization in allOrg:
print("Name: {} \t ID: {}".format(Organization["name"],Organization["id"]))

print('********************************************************************')
print('Listing Networks each Organization')
print('********************************************************************')
print('EU Networks')

url = "https://api.meraki.com/api/v1/organizations/535660/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))

print('********************************************************************')
print('US Networks')

url = "https://api.meraki.com/api/v1/organizations/419633/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))

print('********************************************************************')
print('APAC Networks')

url = "https://api.meraki.com/api/v1/organizations/671036344478206491/networks"
payload={}
headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
networks = json.loads(response.text)
for Networks in networks:
print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))
print('********************************************************************')

 

RaphaelL
Kind of a big deal
Kind of a big deal

its not pretty. but this is the minimal work to make it run with your current code. What is suggested  Eddysanoli is a better approach though. 

 

Not tested.  

 

Also could be simplified a lot more

 

 

import meraki, requests, time, apikey, json

def PrintNetworks(Orgid):
	url = "https://api.meraki.com/api/v1/organizations/{0}/networks".format(Orgid)
	payload={}
	headers = {'X-Cisco-Meraki-API-Key': apikey.api_key}
	response = requests.request("GET", url, headers=headers, data=payload)
	networks = json.loads(response.text)
	for Networks in networks:
		print("Name: {} \t ID: {}".format(Networks["name"],Networks["id"]))

#Organizations
print('********************************************************************')
print('Listing Organizations xxxx')
print('********************************************************************')
url = "https://api.meraki.com/api/v1/organizations"
payload={}
headers = {  'X-Cisco-Meraki-API-Key': apikey.api_key}
response = requests.request("GET", url, headers=headers, data=payload)
allOrg = json.loads(response.text)
for Organization in allOrg:
    print("Name: {} \t ID: {}".format(Organization["name"],Organization["id"]))
	if Organization["id"] == "535660":
		print('EU Networks')
		PrintNetworks(Organization["id"])
	elif Organization["id"] == "419633":
		print('US Networks')
		PrintNetworks(Organization["id"])
	elif Organization["id"] == "671036344478206491":
		print('APAC Networks')
		PrintNetworks(Organization["id"])
		
print('********************************************************************')

 

Get notified when there are additional replies to this discussion.