cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Issue with Meraki Python Script -Air Marshall

SOLVED
Highlighted
New here

Issue with Meraki Python Script -Air Marshall

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()

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
New here

Re: Issue with Meraki Python Script -Air Marshall

My bad i figured it out sorry. It's Monday i had the wrong API key ugh...

View solution in original post

2 REPLIES 2
Highlighted
New here

Re: Issue with Meraki Python Script -Air Marshall

My bad i figured it out sorry. It's Monday i had the wrong API key ugh...

View solution in original post

Highlighted
Kind of a big deal

Re: Issue with Meraki Python Script -Air Marshall

Glad to hear you found the issue  😃

 

44444.jpg

Nolan Herring | nolanwifi.com
TwitterLinkedIn
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.