Reboot Meraki APs

I have found myself several times over the last couple of months needing to reboot all of the APs within a Meraki network. Sometimes due to changes or sometimes due to them not responding for some reason. There really isn’t a clean way of going through and rebooting them aside from one a time within the console. I thought hey I can make this one better and do it via the API. So I went through and built this script to allow someone to put in the Org id and then it will pull back all of the networks that are in that Org and allow you to choose one to reboot all of the APs. It will ask should it go as fast as possible or would you like to put in a delay so that they all don’t go down at the same time. I’ve tested it a couple of times and everything works as its supposed to. As always I look forward to any comments or updates that I can put into the code to make it better.

https://github.com/undrwatr/MERAKI_AP_REBOOT

As usually my code isn’t fancy or special, just serviceable and able to get done what I need and save me some time and headaches.

Retrieve SSID info from Meraki Wireless Network

I support multiple Meraki Wireless networks and I wanted the ability to pull in pertinent data from the wireless networks that I support. This will allow me to pull in the data and then upload it into the wireless mapping programs and design software. This will also allow me to easily pull the channels, power settings, SSIDs, and other pertinent information. Here is the link to my GitHub site where I will be keeping the most up to date program as I fix things and work to improve it.

#!/usr/bin/env python

#import necessary modules
import cred
import requests
#Meraki site information
MERAKI_DASHBOARD = 'https://api.meraki.com'
HEADERS = {'X-Cisco-Meraki-API-Key': (cred.key), 'Content-Type': 'application/json'}
#NETWORK = input(str("What network are we looking at? "))
NETWORK = cred.network
NETWORK_URL = MERAKI_DASHBOARD + '/api/v0/networks/%s/devices' % NETWORK
NETWORK_GET = requests.get(NETWORK_URL, headers=HEADERS)
NETWORK_RESPONSE = NETWORK_GET.json()
#Create a function pull in the information
def WIRELESS_SETTINGS():
WIRELESS_SETTINGS_URL = MERAKI_DASHBOARD + '/api/v0/networks/%s/devices/%s/wireless/status' % (NETWORK, DEVICE['serial'])
WIRELESS_SETTINGS_GET = requests.get(WIRELESS_SETTINGS_URL, headers=HEADERS)
WIRELESS_SETTINGS_RESPONSE = WIRELESS_SETTINGS_GET.json()
for SSIDS in WIRELESS_SETTINGS_RESPONSE['basicServiceSets']:
if SSIDS['enabled'] == True:
print("SSID " + (SSIDS['ssidName']) + " BAND " + (SSIDS['band']) + " BSSID " + str(SSIDS['bssid']) + " Channel " + str(SSIDS['channel']) + " Power " + str(SSIDS['power']))
#Loops through the network and the devices to find all of the information.
for DEVICE in NETWORK_RESPONSE:
if DEVICE['model'] == "MR42":
print("AP " + DEVICE['name'])
WIRELESS_SETTINGS()

Moving from Pymssql to Pyodbc

I was in the process of updating one of my linux servers with Pymssql when I kept getting an error message about it trying to do the install:

ERROR: Could not find a version that satisfies the requirement pymmssql (from versions: none)
ERROR: No matching distribution found for pymmssql

Turns out pymmsql had been deprecated and was no longer being supported. I use pymmsql to connect to my sql database in order to pull data for ip addresses and other network specific information to build my stores in the Meraki Portal.

For the most part the conversion was fairly easy, I think the biggest issue I had was in getting the drivers installed for MS SQL for pyodbc. This site from MS was helpful in installing the ODBC drivers on Mac and Linux. Once I got the drivers installed I then had some issues with making https calls via my python programs. I had to reinstall python via pyenv reinstall. Once the version was reinstalled then it resolved the issues with the API calls and the requests module. Here is an example of the code I was running and the code that I am now running.

PYMSSQL CODE:

import pymssql as mdb

sql_host = cred.sql_host
sql_username = cred.sql_username
sql_password = cred.sql_password
sql_database = cred.sql_database
store = str(input(“What store are we creating?: “))
sql_connection = mdb.connect(sql_host,sql_username,sql_password,sql_database)
cursor = sql_connection.cursor()
cursor.execute(“select [VID2GW] from tblDSlip where [Store #] = (%s)”, (store))
VLAN2GW = str(cursor.fetchone()[0])

PYODBC CODE:

import pyodbc
store = str(input(“What store are we creating?: “))

if len(store) == 2:
sql_store = ’00’ + store
elif len(store) == 3:
sql_store = ‘0’ + store
else:
sql_store = store

server = cred.sql_host
database = cred.sql_database
username = cred.sql_username
password = cred.sql_password

cnxn = pyodbc.connect(‘DRIVER={ODBC Driver 17 for SQL Server};SERVER=’+server+’;DATABASE=’+database+’;UID=’+username+’;PWD=’+ password)
cursor = cnxn.cursor()
cursor.execute(“select [VID2GW] from tblDSlip where [Store #] = ?”, sql_store)
VLAN2GW = str(cursor.fetchone()[0])

 

Date/Time Conversion in Python – ISO 8601 UTC to Pacific Standard Time

I had an issue where I was getting an ISO 8601 UTC date back from one of my API requests and I was asked to convert it to Pacific time as I was providing the information in a report. Since I had no idea what I was doing I first had to figure out what kind of date I was dealing with so that I could then figure out what I was trying to get to.

Here is what an ISO 8601 date looks like:  2019-12-24T15:04:18Z


Here is what the format of the data will look like after the process has been run: 12-24-2019 7:04:18

#Modules needed
import datetime
import pytz

#set the variable for Pacific timezone
pst = pytz.timezone(‘US/Pacific’)

#create the time variable date_time that will be used throughout, the data is coming from an api request and I am getting back json data.
date_time = (ISO 8601 Date)
#set the format for the date time
date_time = datetime.datetime.strptime(date_time, “%Y-%m-%dT%H:%M:%SZ”)
#tell python the current timezone is UTC
date_time = pytz.timezone(‘UTC’).localize(date_time)
#tell python to change the timezone to Pacific
date_time = date_time.astimezone(pst)
#update the variable with the new time and date in the new format
date_time = date_time.strftime(“%m-%d-%y %H:%M:%S”)