API Call reboot MX

MarcP
Kind of a big deal

API Call reboot MX

Hey,

 

is there any chance to reboot a MX65 using API call?

 

After a short Firewall breakdown hundreds of Cisco APs (classic) are not connected to the Controller anymore.

Tried to solve it by rebooting the MX 65 on site of the location. 

After the MX rebooted the AP was able to connect to the controller.

 

Problem now, I don´t want to restart ~200 MX´s step by step. instead I would like to do it by API, but I can´t find anything about it.

Only can see a community thread for rebooting APs, but there was no real reboot of the device, only ports. As our APs are connected to Cisco SF300 switches this won´t be possible for me.

6 Replies 6
BrechtSchamp
Kind of a big deal

Yes you can.

 

You can probably base your script on the one I posted here:

https://community.meraki.com/t5/Security-SD-WAN/Combined-Network-of-MR33-and-MX65-I-have-multiple-ne...

Nash
Kind of a big deal

Sure can: https://dashboard.meraki.com/api_docs#reboot-a-device

 

I'd probably:

 

  1. IF you have multiple orgs, pull a list of orgs
  2. Pull a list of networks for your org
  3. Pull a list of devices
  4. Check if a device is an MX
  5. If MX, add serial to list
  6. For device in list, send reboot command. 🙂

 

I've got some Python code snippets I can throw into a github if you're not sure how to execute this.

 

Edit: Or @BrechtSchamp can beat me to the punch after help desk interrupts me!

MarcP
Kind of a big deal

Thx Guys, will try later or tomorrow 🙂

SoCalRacer
Kind of a big deal

@MarcP Below is some workable code for a single MX reboot using the API/Python. Verified it is working on rebooting MX and APs. I don't like that there is nothing in the event logs showing the reboot, but it gets the job done. Obviously you can parse through a list of devices if your project requires that.

 

from meraki.meraki import Meraki

# Configuration parameters and credentials
x_cisco_meraki_api_key = 'your_api_key_here'

client = Meraki(x_cisco_meraki_api_key)

collect = {}

network_id = 'your_network_id_here'
collect['network_id'] = network_id

serial = 'your_device_serial_here'
collect['serial'] = serial

result = client.devices.reboot_network_device(collect)

print(result)
MarcP
Kind of a big deal

Ok, tried it and it worked finally (with help of colleagues, as I had Problems with SDK and so no, never used python before).

 

Tried it with single networks so far, need to learn more of this before trying Brechts script for multiple networks.

 

So, thanks a lot guys, your comments were very helpful, as always 😉

MarcP
Kind of a big deal

Whoop Whoop, finally made it working...

 

Always had problem, now I got it, after changing the following:

 

Original:

        # loop over the networks
        if networks:  # make sure it's not an empty organization
            for network in networks:

                # create a devicescontroller to fetch devices
                devicescontroller = client.devices

                # fetch a list of devices in the specified network
                devices = devicescontroller.get_network_devices(network['id'])

                # prepare the devicescollection to reboot them
                devicescollect = {}

                for device in devices:

                    # only interested in MX devices
                    if 'MX' in device['model']:
                        devicescollect['serial'] = device['serial']
                        devicescollect['network_id'] = network['id']

                    # reboot the device
                    if devicescollect:  # make sure there are devices to reboot
                        result = devicescontroller.reboot_network_device(devicescollect)

                        if result['success']:
                            print("The device with the name ", device['name'], "(", device['model'],
                                  ") in the network ", network['name'], " was successfully rebooted.")
                        else:
                            print("ERROR: The device with the name ", device['name'], "(", device['model'],
                                  ") in the network ", network['name'], " could not be rebooted.")

 

 

My new setup:

 

        # loop over the networks
        if networks:  # make sure it's not an empty organization
                for network in networks:
                    # create a devicescontroller to fetch devices
                    devicescontroller = client.devices
 
                    # fetch a list of devices in the specified network
                    devices = devicescontroller.get_network_devices(network['id'])
 
                    # prepare the devicescollection to reboot them
                    devicescollect = {}
 
                    for device in devices:
                        # only interested in MX devices
                        if 'MX' in device['model']:
                             devicescollect['serial'] = device['serial']
                             devicescollect['network_id'] = network['id']
                             # reboot the device
                             if devicescollect:  # make sure there are devices to reboot
                                 result = devicescontroller.reboot_network_device(devicescollect)
                                 if result['success']:
                                        print("SUCCESS: Following devices have been rebooted ", network['name'])
                                 else:
                                        print("ERROR: Following device could not been rebooted ", network['name'])


Only at the End, the results / print messages have to be more to the right, as otherwise the script wants to reboot (in my organization) MR´s on site as well. I´m sure if I would have MS´s in there, they would be restartet as well.

 

In my case, who wants to reboot MX´s thats fine. but you could restart only MS´s / MS´s now as well.

 

 

 

Complete Script:

 

 
from meraki.meraki import Meraki

# user API key
x_cisco_meraki_api_key = 'your API Key'


try:
    client = Meraki(x_cisco_meraki_api_key)

    # get a list of organizations the user has access to
    orgs = client.organizations.get_organizations()

    # prepare the orgcollect to be used in the get_organization_networks call
    orgcollect = {}

    for org in orgs:
        #print(org['id'])
        # only interested in a specific organization
        if org['id'] == 123456: # Your Org here
            orgcollect['organization_id'] = org['id']

    # fetch all networks in the organization(s)
    if orgcollect:  # make sure it's not an empty collection
        networks = client.networks.get_organization_networks(orgcollect)

        # loop over the networks
        if networks:  # make sure it's not an empty organization
            for network in networks:
                # create a devicescontroller to fetch devices
                devicescontroller = client.devices

                # fetch a list of devices in the specified network
                devices = devicescontroller.get_network_devices(network['id'])

                # prepare the devicescollection to reboot them
                devicescollect = {}

                for device in devices:
                    # only interested in MX devices
                    if 'MX' in device['model']:
                        devicescollect['serial'] = device['serial']
                        devicescollect['network_id'] = network['id']
                        # reboot the device
                        if devicescollect:  # make sure there are devices to reboot
                            result = devicescontroller.reboot_network_device(devicescollect)
                            if result['success']:
                                print("SUCCESS: Following devices have been rebooted ", network['name'])
                            else:
                                print("ERROR: Following device could not been rebooted ", network['name'])
        else:
            print("Empty Organization")
    else:
        print("No organizations selected")
except:
    print("Exception occurred, possibly and invalid API key.")

 

Get notified when there are additional replies to this discussion.
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.
Labels