On the Meraki API Dashboard API in Cisco Devnet, how can I configure multiple devices at one time?

Solved
Jack-Yi
Comes here often

On the Meraki API Dashboard API in Cisco Devnet, how can I configure multiple devices at one time?

For example, I have multiple MS switches in a network and I would like to configure them all at a time via a script other than do it one at a time on the Dashboard.

If I configure a individual device such as a MS switch on the Dashboard via API, I have no problem at all. 

1 Accepted Solution
rhbirkelund
Kind of a big deal

You can't use the "Postman Lite" on the Meraki API Documentation for configuring multiple ports at a time. The "postman lite" on meraki.io only serves as a demo/quick test of the API Endpoint.

 

If you need to configure multiple ports at a time, you either need to look into scripting using e.g. Python, or install Postman locally on your machine. I'm told you can make loops in Postman, so you can configure more than one port at a time, but I have no experience with Postman, so you'll most likely be on your own, or ask somebody else for help.

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.

View solution in original post

6 Replies 6
PhilipDAth
Kind of a big deal
Kind of a big deal

I'm not sure what you are asking, but if you can configure one script via API already, then you can just iterate through all the switches using the same process.

Thanks for replay!  On the Dashboard is shown as below:

JackYi_1-1677258227094.png

It only allows you input one Port id and one Serial and it will generate a Json script based on the inputed Port id and Serial. So question is that is there way we can input multiple Port id for a switch or even multiple Serials in this Jason scripts to confiture the multiple ports and multiple switches at one time?

PhilipDAth
Kind of a big deal
Kind of a big deal

Check our @rhbirkelund 's answer, I think he covered it off pretty good.  He even went to the trouble of crating an example for you.

rhbirkelund
Kind of a big deal

Following the payload definition for switch ports, as shown on Meraki API Documentation here; https://developer.cisco.com/meraki/api-v1/#!update-device-switch-port

 

Imagine then that you have defined the switchport configurations for each port on each switch some where, and have it loaded into Python as a dictionary - agian following the payload schema definition as shown in the documentation.

This configuration is exemplified as the MySwitches variable.

Then you'd might do something along the lines of this;

 

#! /usr/bin/env python3

import json
import requests
import time

MySwitches = [
    {
        'serial': 'Qaaa-nnnn-1111',
        'ports': [
            {
                'portId': 1,
                'config': {
                    "name": "My switch port 1",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 2,
                'config': {
                    "name": "My switch port 2",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 3,
                'config': {
                    "name": "My switch port 3",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
        ]
    },
    {
        'serial': 'Qaaa-nnnn-2222',
        'ports': [
            {
                'portId': 1,
                'config': {
                    "name": "My switch port 1",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 2,
                'config': {
                    "name": "My switch port 2",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 3,
                'config': {
                    "name": "My switch port 3",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
        ]
    },
    {
        'serial': 'Qaaa-nnnn-3333',
        'ports': [
            {
                'portId': 1,
                'config': {
                    "name": "My switch port 1",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 2,
                'config': {
                    "name": "My switch port 2",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
            {
                'portId': 3,
                'config': {
                    "name": "My switch port 3",
                    "tags": [ "tag1", "tag2" ],
                    "enabled": True,
                    "poeEnabled": True,
                    "type": "access",
                    "vlan": 10,
                    "voiceVlan": 20,
                    "allowedVlans": "1,3,5-10",
                    "isolationEnabled": False,
                    "rstpEnabled": True,
                    "stpGuard": "disabled",
                    "linkNegotiation": "Auto negotiate",
                    "portScheduleId": "1234",
                    "udld": "Alert only",
                    "accessPolicyType": "Sticky MAC allow list",
                    "accessPolicyNumber": 2,
                    "macAllowList": [],
                    "stickyMacAllowList": [],
                    "stickyMacAllowListLimit": 5,
                    "stormControlEnabled": True,
                    "adaptivePolicyGroupId": "123",
                    "peerSgtCapable": False,
                    "flexibleStackingEnabled": True,
                    "daiTrusted": False,
                    "profile": {
                        "enabled": False,
                        "id": "1284392014819",
                        "iname": "iname"
                        }
                    }
            },
        ]
    },
]

MAX_API_RETRIES = 5

headers = {
    "Content-Type": "application/json",
    "Accept": "application/json",
    "X-Cisco-Meraki-API-Key": "sherlocked"
}
for switch in MySwitches:
    for port in switch['ports']:
        payload = json.dumps(port['config'])
        url = f"https://api.meraki.com/api/v1/devices/{switch['serial']}/switch/ports/{port['portId']}"
        #print(switch['serial'],payload)
        # print(url,"\t",payload)
        for _ in range(MAX_API_RETRIES):
            try:
                response = requests.request('PUT', url, headers=headers, data = payload)
                if response.ok:
                    print(response.json())
                elif response.status_code == 429:
                    time.sleep(int(response.headers["Retry-After"]))
                else:
                    print(f"Unhandled HTTP error {response.status_code}")
            except Exception as e:
                print(f"Exception caught: {e}")

 

Edited: Updated the script to handle retries and API ratelimiting.

 

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.

Thanks for replay.  My question is based on as below image:

JackYi_0-1677257276493.png

It only allows you input one Port id and one Serial on the Dashboard, which means you can only configure the port on a switch one at a time.  For example, how can I input multiple Port id here to configure multiple ports at a time on a switch?

rhbirkelund
Kind of a big deal

You can't use the "Postman Lite" on the Meraki API Documentation for configuring multiple ports at a time. The "postman lite" on meraki.io only serves as a demo/quick test of the API Endpoint.

 

If you need to configure multiple ports at a time, you either need to look into scripting using e.g. Python, or install Postman locally on your machine. I'm told you can make loops in Postman, so you can configure more than one port at a time, but I have no experience with Postman, so you'll most likely be on your own, or ask somebody else for help.

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.
Get notified when there are additional replies to this discussion.