Meraki Wireless Dashboard API - How to exclude request on MX not under Advanced Security

Solved
fabferr_SV
Conversationalist

Meraki Wireless Dashboard API - How to exclude request on MX not under Advanced Security

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 :

 

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"])
                ASI = dashboard.appliance.getNetworkApplianceSecurityIntrusion(device["networkId"])
                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()  
 
 
Problem : output indicates after a few org scanned : 400 Bad Request, {'errors': ['Intrusion detection is not supported by this network']}
 
Effectively, organization with MX under Enterprise license doesn't show IDS parameters, so the code is crashing.
 
Question : How to exclude those organizations or MX Enterprise in my code ?
 
Many thanks !

 

1 Accepted Solution
mlefebvre
Building a reputation

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()  

 

View solution in original post

3 Replies 3
sungod
Head in the Cloud

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.

 

mlefebvre
Building a reputation

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()  

 

fabferr_SV
Conversationalist

Thank you sungod and mlefebvre.

except funtion works great. It'll be usefull for us for this case and many others. 😀

Get notified when there are additional replies to this discussion.