0
Network_Devices = dashboard.networks.getNetworkDevices('xxx')

I am working on GET Requests with the Cisco Meraki API and Module in Python. The function above accepts only one 'input' ('xxx') above. Also, it will not accept a list.

Is there any way to automate this request in a python script? I have 50+ inputs I would like to run through the function.

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57

1 Answers1

0

Simply use a for loop. You said you have 50+ input, so you have a couple of options in this case:

  1. Define a list in the .py file that holds all the networkd IDs (Not recommended).
  2. Create a file that contains all the network IDs (Recommended).

I'll illustrate option 2:

I assume you are using Meraki SDK v1.12.0 downloaded by running pip install meraki

from pprint import pprint

import meraki

API_KEY = ""

dashboard = meraki.DashboardAPI(API_KEY)

with open(file=r"network_ids.txt", mode="rt", newline="") as f:
    network_ids = [id.strip() for id in f.read().splitlines() if id.strip()]

for network_id in network_ids:
    network_device = dashboard.networks.getNetworkDevices(network_id)
    pprint(network_device)

The network_ids.txt file should be like: (each id on a seperate line)

L_123456
L_789101
L_111213

To minimize the step of collecting network IDs and adding each ID on a seperate line in a file, you can get the IDs from an organization ID:

from pprint import pprint

import meraki

API_KEY = ""

dashboard = meraki.DashboardAPI(API_KEY)

# Any organization id
org_id = "549236"

# Get all networks for an organization
networks = dashboard.organizations.getOrganizationNetworks(org_id, total_pages="all")
# pprint(networks)

# loop through networks
for network in networks:
    # Get network devices for each network ID
    network_devices = dashboard.networks.getNetworkDevices(network["id"])
    # Check if the network has devices
    if network_devices:
        print(f"*** Devices in Network: {network['name']} ({network['id']}) ***")
        for device in network_devices:
            pprint(device)
    else:
        print(
            f"!!! No devices found for network: {network['name']} ({network['id']}) !!!"
        )

Tes3awy
  • 2,166
  • 5
  • 29
  • 51