Med nedenstående Python 3 script, kan man reboote Meraki enheder via API kald.
#!/usr/bin/python3
import csv
import json
import sys
import requests
import time
import email
import smtplib
from email.mime.text import MIMEText
cisco_meraki_api_Key = ‘api key goes here’
organizationId = ‘OrgID goes here’
baseUrl = ‘https://api.meraki.com/api/v0/’
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,
headers=headers,
)
# Parse the get_inventory into json
inventory_json = get_inventory.json()
networkID = “”
serial = “”
# Opens or create a file name results
f = open(‘/home/appliance/scripts/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:
try:
if ‘-ButiksAP’ in ap[‘name’]:
networkID = ap[‘networkId’]
serial = ap[‘serial’]
ap_name = ap[‘name’]
reboot_api_call = requests.post(
baseUrl+’/networks/{}/devices/{}/reboot’.format(networkID, serial ),
headers=headers)
if reboot_api_call.status_code == 200:
print(‘Rebooting —>’, ap_name, ‘—> Successful’ , file=f)
print(“—————————————————“,file=f)
if reboot_api_call.status_code == 400:
print (‘Rebooting —>’, ap_name, ‘—> Bad Request’ , file=f)
print(“—————————————————“,file=f)
if reboot_api_call.status_code == 403:
print (‘Rebooting —>’, ap_name, ‘—> Forbidden’ , file=f)
print(“—————————————————“,file=f)
if reboot_api_call.status_code == 404:
print (‘Rebooting —>’, ap_name, ‘—> Not Found’ , file=f)
print(“—————————————————“,file=f)
if reboot_api_call.status_code == 429:
print (‘Rebooting —>’, ap_name, ‘—> Too Many Requests’ , file=f)
print(“—————————————————“,file=f)
time.sleep(2)
except:
continue
# closes the file
f.close()
#Set up the SMTP server info
SMTP_SERVER = “smtp server”
SMTP_PORT = “smtp port”
#Fill out To/From/Subject fields
EMAIL_TO = “mail@from”
EMAIL_FROM = “mail@to”
EMAIL_SUBJECT = “Text goes here”
#Open log file and pull info
#TODO truncate output so we only get the necessary info from the log file, currently pulls ENTIRE log into email
LOG = open(‘/home/appliance/scripts/results.txt’)
DATA = LOG.read()
#Send email function using smtplib
def send_email():
msg = MIMEText(DATA)
LOG.close()
msg[‘Subject’] = EMAIL_SUBJECT
msg[‘To’] = (EMAIL_TO)
msg[‘From’] = EMAIL_FROM
mail = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
mail.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
mail.quit()
#main
if __name__==’__main__’:
send_email()
No responses yet