Change Meraki Wi-Fi password via API

Nandrade
Conversationalist

Change Meraki Wi-Fi password via API

Hello All,

 

I wanted to share a method I’ve been using to successfully rotate our Public Wi-Fi password across all networks within the organization. Below is an example of JSON code combined with a Python script that can be leveraged to update the Wi-Fi password for a specific SSID across multiple networks within Meraki's dashboard.

 

Variables to Change:

  1. Your-Organization-ID: Replace this with your Meraki organization ID. Find your organization ID
  2. Your-API-Secret-Key: Replace this with your Meraki API key. Obtaining your Meraki API key 

 

 

Python Script for Wi-Fi Password Rotation:

 

 

import requests
import json
import urllib.parse

# BASE API URL WITH ORGANIZATION ID
NetworkIDurl = "https://api.meraki.com/api/v1/organizations/Your-Organization-ID/networks"

# CREATING VARIABLES TO BE USED FOR API REQUESTS
payload = {}
headers = {
    'X-Cisco-Meraki-API-Key': 'Your-API-Secret-Key'
}

# GET REQUEST TO LOAD API DATA
getNetworkID = requests.get(NetworkIDurl, headers=headers, data=payload)

# LOADS JSON DATA INTO VARIABLE[NetworkData] TO BE USED BELOW
NetworkData = json.loads(getNetworkID.text)

# CREATING NETWORK ID LIST TO HOUSE ALL UNIQUE NETWORK ID'S
NetworkID_List = []

# FOR LOOP TO CREATE VARIABLES THAT MATCH API KEYS TO BE USED IN IF STATEMENT
for Network in NetworkData:
    NetworkID = Network["id"]
    ProductType = Network["productTypes"]
    NetworkName = Network["name"]
    # CHECKS IF THE NETWORK HAS "WIRELESS" IN THE PRODUCT TYPE KEY, IF IT DOES ADD IT TO THE NetworkID_List
    if "wireless" in ProductType:
        Network_item = {'ID': NetworkID, 'NAME': NetworkName}
        NetworkID_List.append(Network_item)

# CREATING VARIABLES FOR THE API URL WITH NETWORK ID'S FROM NetworkID_List
ssidURLA = "https://api.meraki.com/api/v1/networks/"
ssidURLGet = "/wireless/ssids"
ssidNetworkNumber = "/wireless/ssids/{ssid_number}?psk="  # PLACEHOLDER FOR SSID NUMBER

# REQUESTING THE NEW SSID PASSWORD
getPSK = input("Please enter the new password for SSID: ")

# SPLITS URL INTO COMPONENTS TO ALLOW FOR SPECIAL CHARACTERS IN VARIABLE [GETpsk]
newPSK = urllib.parse.quote_plus(getPSK)

# ASKS THE USER WHICH SSID NAME THEY WANT TO UPDATE
ssid_name_to_update = input("Please enter the SSID name you want to update: ")

# FOR LOOP TO PROCESS NETWORKS AND SSIDs
for Network_Item in NetworkID_List:
    # GET SSID data for this network
    ssidGET = ssidURLA + str(Network_Item["ID"]) + ssidURLGet
    getSSID = requests.get(ssidGET, headers=headers, data=payload)

    # LOADS JSON DATA INTO VARIABLE[WiFiData] TO BE USED BELOW
    WiFiData = json.loads(getSSID.text)

    # SEARCHING FOR THE SSID THAT MATCHES THE USER-PROVIDED NAME
    ssid_found = False
    for ssid_index, WiFi in enumerate(WiFiData):
        SSIDName = WiFi["name"]
        
        # CHECKS IF SSID NAME MATCHES THE ONE THE USER PROVIDED
        if SSIDName == ssid_name_to_update:
            ssid_found = True
            # BUILD URL TO UPDATE SSID PASSWORD
            ssidPUT = ssidURLA + str(Network_Item["ID"]) + ssidNetworkNumber.format(ssid_number=ssid_index) + newPSK
            
            # PRINT THE PUT URL FOR VERIFICATION
            print(f"Generated PUT URL for SSID '{SSIDName}' in network {Network_Item['NAME']}:")
            print(str(ssidPUT))
            
            # UNCOMMENT THE LINES BELOW TO SEND THE REQUEST
            #putSSID = requests.put(ssidPUT, headers=headers, data=payload)
            #print(f"Password for SSID '{SSIDName}' in network {Network_Item['NAME']} has been updated.")
            break  # BREAK THE LOOP ONCE THE SSID IS FOUND

    if not ssid_found:
        print(f"SSID '{ssid_name_to_update}' not found in network '{Network_Item['NAME']}'.")

 

 

 

 

 

 

 

 

 

 

NANDRADE
1 Reply 1
rhbirkelund
Kind of a big deal
Kind of a big deal

Thanks for the contribution! 😄

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.