Category: Meraki

  • Enhanced Python script for Meraki Switch with VLAN deletion or port monitoring

    • โœ… VLAN creation
    • โŒ VLAN deletion
    • ๐Ÿ”ง Switch port config
    • ๐Ÿ‘€ Port monitoring (get port status like usage, errors)

    ๐Ÿงฐ Full Python Script โ€“ Meraki Switch Automation

    import requests

    # Config
    API_KEY = "YOUR_MERAKI_API_KEY"
    ORG_ID = "YOUR_ORG_ID"
    NETWORK_ID = "YOUR_TEMPLATE_BOUND_NETWORK_ID"
    DEVICE_SERIAL = "YOUR_SWITCH_SERIAL" # Example: Q2XX-XXXX-XXXX

    BASE_URL = "https://api.meraki.com/api/v1"

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

    # --- VLAN FUNCTIONS ---

    # โœ… 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"[CREATE VLAN {vlan_id}] Status: {response.status_code} - {response.text}")

    # โŒ Delete VLAN
    def delete_vlan(vlan_id):
    url = f"{BASE_URL}/networks/{NETWORK_ID}/vlans/{vlan_id}"
    response = requests.delete(url, headers=HEADERS)
    print(f"[DELETE VLAN {vlan_id}] Status: {response.status_code} - {response.text}")

    # --- PORT FUNCTIONS ---

    # ๐Ÿ”ง Configure Switch Port
    def configure_switch_port(port_number, port_name="Trunk Port", allowed_vlans="1,10,20"):
    url = f"{BASE_URL}/devices/{DEVICE_SERIAL}/switch/ports/{port_number}"
    payload = {
    "name": port_name,
    "type": "trunk",
    "vlan": 1,
    "allowedVlans": allowed_vlans,
    "poeEnabled": True,
    "rstpEnabled": True,
    "stpGuard": "disabled"
    }
    response = requests.put(url, headers=HEADERS, json=payload)
    print(f"[CONFIGURE PORT {port_number}] Status: {response.status_code} - {response.text}")

    # ๐Ÿ‘€ Get Port Status
    def get_port_status():
    url = f"{BASE_URL}/devices/{DEVICE_SERIAL}/switch/ports"
    response = requests.get(url, headers=HEADERS)
    if response.status_code == 200:
    ports = response.json()
    for port in ports:
    print(f"Port {port['portId']}: {port.get('enabled', 'N/A')} | "
    f"Type: {port.get('type')} | VLAN: {port.get('vlan')} | "
    f"Usage: {port.get('usageInKb', 'N/A')} Kbps")
    else:
    print(f"[GET PORT STATUS] Error: {response.status_code} - {response.text}")

    # --- EXECUTION ---

    # VLAN Management
    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")
    # delete_vlan(20) # Uncomment to delete VLAN 20

    # Port Configurations
    configure_switch_port(1)
    configure_switch_port(2, port_name="Uplink to Router")

    # Port Monitoring
    get_port_status()




    โœ… Output Example:

    [CREATE VLAN 10] Status: 201 - {"id":"10",...}
    [CONFIGURE PORT 1] Status: 200 - { ... }
    Port 1: True | Type: trunk | VLAN: 1 | Usage: 120 Kbps


    ๐Ÿ›ก๏ธ Recommendations:

    • Use environment variables or config files for sensitive info (API keys).
    • Add retry logic or logging for production scripts.
    • Use the Meraki Python SDK (meraki package) if you prefer an abstracted interface.
  • 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:

    • API key from Meraki Dashboard (keep it secret!)
    • Template-bound Network ID
    • requests library installed (pip install requests)

    ๐Ÿง  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

    • Change NETWORK_ID and DEVICE_SERIAL to match your environment.
    • You can loop through multiple ports/VLANs for bulk updates.
    • Add error handling for production use.
  • How to Create and Modify Meraki Switch Templates

    ๐Ÿงฉ 1. What is a Meraki Switch Template?

    In Cisco Meraki, templates are configurations that can be applied across multiple networks, especially useful in large-scale deployments to ensure consistency.

    • A switch template allows you to configure:
      • VLANs
      • Port settings
      • STP settings
      • QoS policies
      • Link aggregation
      • Access policies (802.1X)
      • Voice VLANs
      • PoE settings

    ๐Ÿ–ฅ๏ธ 2. Creating a Switch Template (via GUI)

    • Login to the Meraki Dashboard
    • Go to:
      Organization > Configuration templates
    • Click โ€œCreate a new templateโ€
    • Name your template (e.g., Branch-Switch-Template)
    • Click Create
    • Click on the template name โ†’ Switch tab
    • Configure:
      • VLANs under Switch settings > Routing and DHCP
      • Per-port settings via Switch ports
      • QoS, STP, etc. under Switch settings
    • Bind networks to this template under:
      Organization > Configuration templates > Template > Bind networks

    โš™๏ธ 3. Modifying a Switch Template (via GUI)

    • Go to:
      Organization > Configuration templates > [Your Template]
    • Under the Switch tab, modify:
      • VLANs
      • Switch port configs
      • Layer 3 interfaces
      • Access policies
    • Changes auto-apply to all bound networks

    ๐Ÿ”ง 4. Creating/Modifying Switch Templates (via API)

    ๐Ÿ“Œ Prerequisites:

    • Dashboard API key
    • Network ID or template ID
    • API base URL: https://api.meraki.com/api/v1

    โœ… Create a Configuration Template:

    POST /organizations/{organizationId}/configTemplates

    {
    "name": "Branch Switch Template"
    }

    โœ… Modify VLAN Settings in a Template:

    PUT /networks/{networkId}/switch/settings

    {
    "vlan": 20,
    "useCombinedPower": true,
    "voiceVlanId": 100
    }

    โœ… Update Switch Port:

    PUT /devices/{serial}/switch/ports/{portId}

    {
    "name": "Uplink Port",
    "type": "trunk",
    "vlan": 1,
    "allowedVlans": "1,10,20",
    "poeEnabled": true
    }

    ๐Ÿ“š Extra Notes:

    • Templates can be bound to multiple networks. Once bound, you cannot configure those networks individually, unless you unbind them.
    • Best Practice: Create staging/testing networks to validate templates before applying widely.
  • How to enable Meraki API

    The Meraki API allows you to automate and manage your Meraki network programmatically. It is a RESTful API that provides access to Meraki dashboard data and configurations.


    1. Enable Meraki API

    Before using the API, you must enable it in the Meraki Dashboard:

    • Login to Meraki Dashboard โ†’ dashboard.meraki.com
    • Go to “Organization” > “Settings”
    • Scroll to “Dashboard API access” and toggle it ON.
    • Click “Save”.

    2. Generate an API Key

    • Go to “My Profile” (top-right corner in the dashboard).
    • Scroll to “API access”.
    • Click “Generate API Key”.
    • Copy and save the API Key (It wonโ€™t be shown again).

    โš ๏ธ Security Note: Treat your API key like a passwordโ€”never expose it in public or store it in unsecured locations.


    3. Test API Connectivity

    Use Postman or cURL to test API access.

    Example: Get Organization List

    Using cURL:

    curl -L -H "X-Cisco-Meraki-API-Key: YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -X GET "https://api.meraki.com/api/v1/organizations"

    Using Python:

    import requests

    API_KEY = "YOUR_API_KEY"
    url = "https://api.meraki.com/api/v1/organizations"

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

    response = requests.get(url, headers=headers)
    print(response.json())

    4. Common Meraki API Endpoints

    Here are some useful endpoints:

    Organization Management

    ActionMethodEndpoint
    List organizationsGET/organizations
    Get org detailsGET/organizations/{orgId}
    Claim devices to orgPOST/organizations/{orgId}/claim

    Network Management

    ActionMethodEndpoint
    List networksGET/organizations/{orgId}/networks
    Create a networkPOST/organizations/{orgId}/networks
    Delete a networkDELETE/networks/{networkId}

    Device Management

    ActionMethodEndpoint
    List devices in a networkGET/networks/{networkId}/devices
    Get device detailsGET/devices/{serial}
    Update device settingsPUT/devices/{serial}

    5. Advanced Usage

    • Meraki Python SDK: Use the official Meraki Python library for easier API calls. bashCopyEditpip install meraki pythonCopyEditimport meraki API_KEY = "YOUR_API_KEY" dashboard = meraki.DashboardAPI(API_KEY) orgs = dashboard.organizations.getOrganizations() print(orgs)
    • Webhooks: Configure webhooks for real-time alerts.
    • Automation: Automate network provisioning, VLAN assignments, SSID management, etc.

    6. API Documentation

    For a complete list of API endpoints, visit Meraki API Docs.

  • Create a New Organization in Meraki Cloud

    Creating an organization in the Meraki Dashboard is the first step to managing your Meraki network. Follow these steps to create a new organization:

    Steps to Create a New Organization in Meraki Cloud

    • Sign in to Meraki Dashboard
      • Go to Meraki Dashboard.
      • Log in with your Cisco Meraki credentials.
      • If you donโ€™t have an account, click “Create an Account” and follow the setup process.
    • Create a New Organization
      • In the dashboard, click on your account name in the top-right corner.
      • Select “Organizations” > “Create Organization”.
      • Enter the organization name of your choice.
    • Add a Network to the Organization
      • Once the organization is created, click “Create a Network”.
      • Choose the network type (e.g., Wireless, Security Appliance, Switches, etc.).
      • Name your network and select the appropriate settings.
    • Claim Devices (Optional but Recommended)
      • Go to “Organization” > “Inventory”.
      • Click “Claim” and enter the serial number(s) of your Meraki devices.
      • Alternatively, if you purchased through a reseller, they might have assigned the devices to your organization automatically.
    • Configure Licensing
      • Navigate to “Organization” > “License Info”.
      • Click “Add License” and enter the Meraki license key associated with your devices.
      • Assign the license to your organization.
    • Finalize Setup
      • Verify that your organization and network are set up correctly.
      • Go to “Network-wide” > “General” and adjust settings as needed.
      • Start configuring your devices (SSID, firewall rules, VLANs, etc.).

    Additional Tips

    • If you are managing multiple organizations, you can switch between them using the organization dropdown in the top-left corner.
    • Use Meraki API to automate network provisioning if needed.
    • If you’re a Meraki MSP (Managed Service Provider), you can create and manage multiple organizations for clients.