Hi,
I'm beginning in Python code with Meraki's API. We are a MSP so we have multiple Organizations.
Made a few codes, but i'm stuck on this one :
Want to list in to a csv file, organizations where a MX under Advanced Security is configured, and have the
IDS mode value.
Code :
Solved! Go to solution.
Use try/except error handling. In this example, when the first network in the org throws an error in the getNetworkApplianceSecurityIntrusion call, the try/except/break will exit the for device in devices loop, which will then make your code skip to the next organization
import meraki
import os
API = os.environ.get("API_KEY_FF")
dashboard = meraki.DashboardAPI(API, suppress_logging=True)
to_write = ""
organisations = dashboard.organizations.getOrganizations()
for organisation in organisations:
devices = dashboard.organizations.getOrganizationDevices(organisation["id"], total_pages='all')
for device in devices:
if "MX" in device["model"]:
print(organisation["name"] + "," + device["model"])
try:
ASI = dashboard.appliance.getNetworkApplianceSecurityIntrusion(device["networkId"])
except Exception as e:
print(str(e))
break
for MX in ASI:
to_write = to_write + f'{organisation["name"]},{device["networkId"]},{device["name"]},{ASI["mode"]}\n'
f = open("Get_ASI_MX.csv", "w")
f.write(to_write)
f.close()
If a feature or other entity is not available, the API will often return some kind of 4xx error. Like trying to do a MX call on an SM network.
One way of handling is to catch the error and check if the message is the one you mention, then skip the org. Look at Python exception handling, for example...
https://realpython.com/python-exceptions/
To avoid the error in the first place, you need to ensure the request is valid in the context you make it, you could do this by getting the org licences first, using...
https://developer.cisco.com/meraki/api-v1/get-organization-licenses/
...then checking that the licence type is correct, if not then skip the org.
Use try/except error handling. In this example, when the first network in the org throws an error in the getNetworkApplianceSecurityIntrusion call, the try/except/break will exit the for device in devices loop, which will then make your code skip to the next organization
import meraki
import os
API = os.environ.get("API_KEY_FF")
dashboard = meraki.DashboardAPI(API, suppress_logging=True)
to_write = ""
organisations = dashboard.organizations.getOrganizations()
for organisation in organisations:
devices = dashboard.organizations.getOrganizationDevices(organisation["id"], total_pages='all')
for device in devices:
if "MX" in device["model"]:
print(organisation["name"] + "," + device["model"])
try:
ASI = dashboard.appliance.getNetworkApplianceSecurityIntrusion(device["networkId"])
except Exception as e:
print(str(e))
break
for MX in ASI:
to_write = to_write + f'{organisation["name"]},{device["networkId"]},{device["name"]},{ASI["mode"]}\n'
f = open("Get_ASI_MX.csv", "w")
f.write(to_write)
f.close()
Thank you sungod and mlefebvre.
except funtion works great. It'll be usefull for us for this case and many others. 😀