Showing results for 
Search instead for 
Did you mean: 

Python Script - Rebooting Devices

Kind of a big deal

Python Script - Rebooting Devices

Hey everyone


I worked with a team member of mine to come up with an automated script that runs on our Ansible server that will reboot whichever Meraki access point that has 'API' at the end of its name (or really just in the name), on a weekly scheduled basis, with email reporting.  This could be used I'm sure for MX or MS as well, but we are only using it for access points.


Feel free to critique. I'm not a Python person by any means, but this works great:


import json
import sys
import requests
import time
import email
import smtplib

organizationId = 'XXXXXX'
baseUrl = ''
inventory_api_url = "organizations/{}/inventory".format(organizationId)

headers = {
    'X-Cisco-Meraki-API-Key': cisco_meraki_api_Key,
    'Content-Type': 'application/json'

get_inventory = requests.get(baseUrl+inventory_api_url,

# Parse the get_inventory into json
inventory_json = get_inventory.json()

networkID = ""
serial = ""

# Opens or create a file name results
f = open('results.txt', "w+")

# loop over all the dictionaries inside inventory_json,
# if API is inside the dictionary it will get the NetworkID and the serial and then write it to the string above

for ap in inventory_json:
        if 'API' in ap['name']:
            networkID = ap['networkId']
            serial = ap['serial']
            ap_name = ap['name']
            reboot_api_call =
                baseUrl+'/networks/{}/devices/{}/reboot'.format(networkID, serial ),
            if reboot_api_call.status_code == 200:
                print('Rebooting --->', ap_name, '---> Successful' , file=f)
            if reboot_api_call.status_code == 400:
                print ('Rebooting --->', ap_name, '---> Bad Request' , file=f)
            if reboot_api_call.status_code == 403:
                print ('Rebooting --->', ap_name, '---> Forbidden' , file=f)
            if reboot_api_call.status_code == 404:
                print ('Rebooting --->', ap_name, '---> Not Found' , file=f)
            if reboot_api_call.status_code == 429:
                print ('Rebooting --->', ap_name, '---> Too Many Requests' , file=f)

# closes the file

# Setting up the SMTP Server
sender = 'meraki_api@company.local'
receiver = ''

smtp_server = smtplib.SMTP(host='X.X.X.X', port=25)

f = open('results.txt', "r")

# reads the information inside the file results
script_result =

message = """From: <meraki_api@company.local>
To: <>
Subject: Meraki AP Reboot

API CODE: 200 = Successful
API CODE: 400 = Bad Request
API CODE: 403 = Forbidden
API CODE: 404 = Not Found
API CODE: 429 = Too Many Requests


# Email execution
    smtp_server.sendmail(sender, receiver, message)
    print('Successfully sent email')
    print('Error: Unable to send email')

# closes the file.
Nolan Herring |
Head in the Cloud

Re: Python Script - Rebooting Devices

Is this more for just good measure to reboot APs weekly or do you manually change names on the problem APs that you want to get the weekly scheduled reboot?

Kind of a big deal

Re: Python Script - Rebooting Devices

It's good measure/preventative.

Every now and then an AP's 5GHz radio (random) will just get hung up. Never been able to determine why. Has happened on different code versions. Usually takes a few weeks to months for it to crop up. Too much effort to review event logs for every AP (which makes it easy to see if the radio is bugged out or not), so its just easier to reboot them once a week so they never have enough 'bake time' to run into it.

MR32 happen much more often vs MR33
Nolan Herring |
Kind of a big deal

Re: Python Script - Rebooting Devices

And to be clear, I've had many cases for these 'bugged out' radios. Answer from support has always been the same, upgrade firmware. Which I think is neat because you know...if you don't know what the root problem is, it can't possibly also exist in the next firmware version right? I gave up after about a year of trying to help them figure it out, so now I just do weekly reboots.
Nolan Herring |
Just browsing

Re: Python Script - Rebooting Devices

Meraki has confirmed an issue with the beacons on the MR16 and are working on the fix. Currently, they are saying no other AP's are having this issue. We have seen similar issues on the MR18's as well however. 


Thanks for the script, we will give it shot and see if it corrects our MR16 problems.

Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.