Loop Through List of Network IDs from txt file

eroche
Getting noticed

Loop Through List of Network IDs from txt file

Ok we have 33 networks and I need to update an alert setting on each to disable. Would like to do this ins one shot. I know with only 33 it would be a lot faster overall to manually do it but looking to the future when this comes up again. 😉

 

 

import meraki

 

API_KEY = ''

 

dashboard = meraki.DashboardAPI(API_KEY)

 

with open("./data/networks.txt", "r") as network:
     for net in network.read().splitlines():
         network_id = {
              "network_id": network
          }

          response = dashboard.networks.updateNetworkAlertsSettings(
               network_id,
               alerts=[{'type': 'vpnConnectivityChange', 'enabled': False,}],
          )

 

          print(response)

 

12 Replies 12
alemabrahao
Kind of a big deal
Kind of a big deal

So you want us to do the work for you? LOL

There seems to be a small mistake in your code. You’re assigning the file object network to the network_id dictionary, instead of the actual network ID net from your loop.

 

"network_id": net

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
eroche
Getting noticed

Hey at least I am giving most of the work LOL.

 

No just beginning my journey in this crazy topic.

 

Thanks I will try that

eroche
Getting noticed

ok so I keep getting this error, almost looks like its not liking the dashboard.networks.updateNetworkAlertsSettings.

 

2024-07-11 16:49:31 meraki: ERROR > networks, updateNetworkAlertsSettings - 404 Not Found, b''
Traceback (most recent call last):
File "/mnt/m2ssd/python/meraki_set_network_alert.py", line 15, in <module>
response = dashboard.networks.updateNetworkAlertsSettings(
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/meraki/api/networks.py", line 149, in updateNetworkAlertsSettings
return self._session.put(metadata, resource, payload)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/meraki/rest_session.py", line 486, in put
response = self.request(metadata, 'PUT', url, json=json)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/usr/local/lib/python3.11/dist-packages/meraki/rest_session.py", line 300, in request
raise APIError(metadata, response)
meraki.exceptions.APIError: networks, updateNetworkAlertsSettings - 404 Not Found, please wait a minute if the key or org was just newly created.

 

alemabrahao
Kind of a big deal
Kind of a big deal

The error 404 Not Found, typically means that the server could not find the requested resource. In the context of the Meraki API, this could mean a few things.

 

 Make sure that the network IDs in your text file are correct and that they correspond to existing networks in your Meraki dashboard.

I am not a Cisco Meraki employee. My suggestions are based on documentation of Meraki best practices and day-to-day experience.

Please, if this post was useful, leave your kudos and mark it as solved.
eroche
Getting noticed

sent a private msg

eroche
Getting noticed

So for giggles I stuck one of the network ids in the script and commented out the file import and it worked. So definitely it is something with the coming in from the file. 🤔

eroche
Getting noticed

So figured the out the issue. Changed the Red to the Blue

 

with open("./data/networks.txt", "r") as network:
     for net in network.read().splitlines():
         network_id = {
              "network_id": network
          }

 

with open("./data/networks.txt", "r") as network:
     for net in network.read().splitlines():

          network_id = net

 

rhbirkelund
Kind of a big deal
Kind of a big deal

You could also simply just use

with open('names.txt','r') as file:
    for line in file:
        print(line.strip())
LinkedIn ::: https://blog.rhbirkelund.dk/

Like what you see? - Give a Kudo ## Did it answer your question? - Mark it as a Solution 🙂

All code examples are provided as is. Responsibility for Code execution lies solely your own.
PhilipDAth
Kind of a big deal
Kind of a big deal

On a different topic, here are a couple of functions that I often use, so that you can just stores the names instead of IDs.

 

def getOrgId(dashboard,orgName):
	# Search for the org
	for org in dashboard.organizations.getOrganizations():
		if org['name'] == orgName:
			return(org['id'])

	raise SystemExit("Invalid organization name supplied: "+orgName)		

 

# This function retrieves the netId
def getNetId(dashboard,orgId,netName):
	# Search for the network
	for net in dashboard.organizations.getOrganizationNetworks(orgId):
		if net['name'] == netName:
			return(net['id'])

	# Search for a template
	for net in dashboard.organizations.getOrganizationConfigTemplates(orgId):
		if net['name'] == netName:
			return(net['id'])

	# Nothing found matching at all
	raise SystemExit("Invalid network name supplied: "+netName)			
eroche
Getting noticed

Thanks much, I like those. It would make the process easier for sure.

eroche
Getting noticed

Hey Phil

 

So I am adding the above functions where in my script:

 

import meraki

 

API_KEY = ''

 

dashboard = meraki.DashboardAPI(API_KEY)

 

with open("./data/networks.txt", "r") as network:
     for net in network.read().splitlines():
         network_id = {
              "network_id": network
          }
HERE?
          response = dashboard.networks.updateNetworkAlertsSettings(
               network_id,
               alerts=[{'type': 'vpnConnectivityChange', 'enabled': False,}],
          )

OR HERE?

          print(response)

 

 

Thanks!

 

PhilipDAth
Kind of a big deal
Kind of a big deal

Yu would do it more in this order.

 

import meraki

def getOrgId(dashboard,orgName):
	# Search for the org
…
# This function retrieves the netId
def getNetId(dashboard,orgId,netName):
	# Search for the network
…


API_KEY = ''
dashboard = meraki.DashboardAPI(API_KEY)
...
Get notified when there are additional replies to this discussion.