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.