03-02-2020 07:39 AM
I have the script below, use to work, stopped working I've been banging my head on it trying to figure it out or is there a different /better way to get this info. Any help you could be would be much appreciated.
import meraki
import json
import requests
import time
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
from datetime import datetime, timezone
#Put your API key here
apikey =
orgid =
def getNetworks(apikey):
url = "https://api.meraki.com/api/v0/organizations/11111/networks"
payload = ""
headers = {
'X-Cisco-Meraki-API-Key': apikey,
'cache-control': "no-cache",
'Postman-Token': ""
}
response = requests.request("GET", url, data=payload, headers=headers)
return response
def getMarshall(network, apikey):
url = "https://api.meraki.com/api/v0/networks/" + network + "/airMarshal"
querystring = {"timespan":"3600"}
payload = ""
headers = {
'X-Cisco-Meraki-API-Key': apikey,
'cache-control': "no-cache",
'Postman-Token': ""
}
response = requests.request("GET", url, data=payload, headers=headers, params=querystring)
return response
def mail():
time = datetime.now().strftime("%m%d%y")
filename = "airmarshallReport_" + time + ".csv"
subject = "Meraki Airmarshall Report"
recipients = ["test@test.com"]
msg = MIMEMultipart()
msg["Subject"] = subject
msg["From"] = "airmarshall@test.com"
msg["To"] = ", ".join(recipients)
part = MIMEBase("application", "octet-stream")
part.set_payload(open(filename, "rb").read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", "attachment; filename=\"" + filename + "\"")
msg.attach(part)
server = smtplib.SMTP("mail.test.com")
server.sendmail("airmarshall@test.com", recipients, msg.as_string())
response = getNetworks(apikey)
networks = response.json()
index1 = 0
index2 = 0
time = datetime.now().strftime("%m%d%y")
with open("airmarshallReport_" + time + ".csv", "w+") as f:
f.write("network,ssid,bssid,contained,devices,channels,firstseen,lastseen,wiredmacs,wiredlastseen\n")
for line in networks:
try:
x = getMarshall(networks[index1]["id"],apikey)
y = x.json()
index = 0
for i in y:
f.write('"' + str(networks[index1]["name"]) + '"' + "," + '"' + str(y[index]["ssid"]) + '"' + "," + '"' + str(y[index]["bssids"][0]["bssid"]) + '"' + "," + '"' + str(y[index]["bssids"][0]["contained"]) + '"' + ",")
try:
for i in y[index]["bssids"][0]["detectedBy"]:
f.write('"' + str(i["device"]) + " RSSI - " + str(i["rssi"]) + " ")
index2 += 1
f.write('"' + ",")
except:
f.write('"' + "none\"," )
try:
f.write('"' + str(y[index]["channels"][0]) + '"' + ",")
except:
f.write('"' + "none\",")
try:
f.write('"' + str(datetime.fromtimestamp(int(y[index]["firstSeen"]))) + '"' + "," + '"' + str(datetime.fromtimestamp(int(y[index]["lastSeen"]))) + '"' + "," + '"' + str(y[index]["wiredMacs"]) + '"' + "," + '"' + str(datetime.fromtimestamp(int(y[index]["wiredLastSeen"]))) + '"' + "\n")
except:
f.write("\"none\",\"none\",\"none\",\"none\"\n")
index += 1
except:
f.write('"' + str(networks[index1]["name"]) + '"' + "," + "\"none\"\n")
index1 += 1
mail()
Solved! Go to Solution.
03-02-2020 07:52 AM
My bad i figured it out sorry. It's Monday i had the wrong API key ugh...
03-02-2020 07:52 AM
My bad i figured it out sorry. It's Monday i had the wrong API key ugh...
03-02-2020 08:06 AM
Glad to hear you found the issue 😃
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide