Automate Meraki Device Renaming


Introduction:

Say goodbye to manual tweaking and hello to automation! Follow along as we guide you through obtaining your Meraki API key, getting your organization ID, and executing the Python script to update device names based on serial numbers or old names.

Getting Started:

  • Enable access to Meraki API Dashboard:

    • Under the organization configuration settings
      OrgConfigSettings
    • Scroll down and you have a check box to Enable API access
      DashboardAPIAccess
  • Obtaining Meraki API Key:

    • Under organization > setting > configure > API & webhooks OR under the check box to Enable API access there is a link to go to the API & webhooks page
    • Click on the left button generate an API key, and copy it for later use in your script.
  • Obtaining you Meraki organization ID with curl:

    run the below curl command just replace the maeraki api key with your key

            
                curl https://api.meraki.com/api/v1/organizations   -L -H 'Authorization: Bearer {Meraki API key}'
            
          
  • Obtaining you Meraki organization ID with Postman:

    • If you prefer using Postman, head to authentication, choose bearer token, and provide your API key for the same information retrieval.

Python Script for Device Renaming:

  • Setting Up CSV File:

    Before diving into the Python script, create a CSV file with device information. You have 2 options

    • Provide the Old name and the new name
    • Provide the new name and the device serial number
  • Updating Device Names by Serial Number:

            
    import requests
    import json
    import csv
    import os  # Import the os module to work with environment variables
    
    # Function to update the name of a device using its serial number
    def update_device_name(serial_num, new_name):
        url = f"https://api.meraki.com/api/v1/devices/{serial_num}"
        payload = json.dumps({
            "name": new_name
        })
        
        # Retrieve the Meraki API token from the environment variable
        meraki_api_token = os.getenv('MERAKI_API_TOKEN')
        
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {meraki_api_token}'
        }
        
        response = requests.request("PUT", url, headers=headers, data=payload)
        print(response)
    
    # Main function to execute the code
    if __name__ == '__main__':
        # Open the CSV file with old and new names
        with open('old_new_names.csv', 'r') as f:
            header = f.readline()
            reader = csv.reader(f)
            
            # Iterate through each row in the CSV file
            for row in reader:
                new_name = row[1]
                serial_num = row[2]
                
                # Update the device name using its serial number
                update_device_name(serial_num, new_name)
                
                print(serial_num)
    
    
    
          

    Updating Device Names by the old name:

            
    import requests
    import json
    import csv
    import os  # Import the os module to work with environment variables
    
    # Function to get the Meraki inventory using the API
    def get_inventory():
        url = "https://api.meraki.com/api/v1/organizations/1496803/devices"
        payload = {}
        
        # Retrieve the Meraki API token from the environment variable
        meraki_api_token = os.getenv('MERAKI_API_TOKEN')
        
        headers = {
            'Authorization': f'Bearer {meraki_api_token}'
        }
        
        response = requests.request("GET", url, headers=headers, data=payload)
        return response.json()
    
    # Function to get the serial number of a device based on its name
    def get_serial_num(name, inventory):
        for device in inventory:
            if device['name'] == name:
                return device['serial']
    
    # Function to update the name of a device using its serial number
    def update_device_name(serial_num, new_name):
        url = f"https://api.meraki.com/api/v1/devices/{serial_num}"
        payload = json.dumps({
            "name": new_name
        })
        
        # Retrieve the Meraki API token from the environment variable
        meraki_api_token = os.getenv('MERAKI_API_TOKEN')
        
        headers = {
            'Content-Type': 'application/json',
            'Authorization': f'Bearer {meraki_api_token}'
        }
        
        response = requests.request("PUT", url, headers=headers, data=payload)
        print(response)
    
    # Main function to execute the code
    if __name__ == '__main__':
        inventory = get_inventory()
        
        # Open the CSV file with old and new names
        with open('old_new_names.csv', 'r') as f:
            header = f.readline()
            reader = csv.reader(f)
            
            # Iterate through each row in the CSV file
            for row in reader:
                old_name = row[0]
                new_name = row[1]
                
                # Get the serial number of the device based on its old name
                serial_num = get_serial_num(old_name, inventory)
                
                # Update the device name using its serial number
                update_device_name(serial_num, new_name)
                
                print(serial_num)
    
            
          


You can watch the video below where I created the script and tested it or on YouTube with this link





Conclusion:

Following these steps, you can efficiently rename your Meraki devices without manual intervention. Automation not only saves time but also reduces the likelihood of errors. Feel free to customize and enhance the script to suit your specific needs. If you have any questions, please don't hesitate to reach out Happy automating!

List of titles