Python script using the Meraki Dashboard API to automate switch port configuration and VLAN creation in a template-bound network.

πŸ” Step 1: Setup

Make sure you have:


🧠 Step 2: Python Script

import requests

# Replace with your Meraki API key
API_KEY = "YOUR_MERAKI_API_KEY"
ORG_ID = "YOUR_ORG_ID"
NETWORK_ID = "YOUR_TEMPLATE_BOUND_NETWORK_ID"
DEVICE_SERIAL = "YOUR_SWITCH_SERIAL" # e.g., Q2XX-XXXX-XXXX

# Meraki base URL
BASE_URL = "https://api.meraki.com/api/v1"

# Headers
HEADERS = {
"X-Cisco-Meraki-API-Key": API_KEY,
"Content-Type": "application/json"
}

# Step 1: Create VLAN
def create_vlan(vlan_id, name, subnet, appliance_ip):
url = f"{BASE_URL}/networks/{NETWORK_ID}/vlans"
payload = {
"id": vlan_id,
"name": name,
"subnet": subnet,
"applianceIp": appliance_ip
}
response = requests.post(url, headers=HEADERS, json=payload)
print(f"VLAN Creation ({vlan_id}):", response.status_code, response.text)

# Step 2: Configure a switch port
def configure_switch_port(port_number):
url = f"{BASE_URL}/devices/{DEVICE_SERIAL}/switch/ports/{port_number}"
payload = {
"name": "Uplink Port",
"type": "trunk",
"vlan": 1,
"allowedVlans": "1,10,20",
"poeEnabled": True,
"rstpEnabled": True,
"stpGuard": "disabled"
}
response = requests.put(url, headers=HEADERS, json=payload)
print(f"Port {port_number} Config:", response.status_code, response.text)

# --- Execution ---
# Create VLANs
create_vlan(10, "Staff", "192.168.10.0/24", "192.168.10.1")
create_vlan(20, "IoT", "192.168.20.0/24", "192.168.20.1")

# Configure ports 1 and 2 as trunk ports
configure_switch_port(1)
configure_switch_port(2)


πŸ“ Customize As Needed

renjithbs Avatar

Posted by

Leave a comment