-
Notifications
You must be signed in to change notification settings - Fork 8
/
cmxreceiver.py
executable file
·110 lines (85 loc) · 3.18 KB
/
cmxreceiver.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!flask/bin/python
"""
Cisco Meraki CMX Receiver
A simple example demonstrating how to interact with the Location Scanning API.
How it works:
- Meraki access points will listen for WiFi clients that are searching for a network to join and log the events.
- The "observations" are then collected temporarily in the cloud where additional information can be added to
the event, such as GPS, X Y coordinates and additional client details.
- Meraki will first send a GET request to this CMX receiver, which expects to receieve a "validator" key that matches
the Meraki network's validator.
- Meraki will then send a JSON message to this application's POST URL (i.e. http://yourserver/ method=[POST])
- The JSON is checked to ensure it matches the expected secret, version and observation device type.
- The resulting data is sent to the "save_data(data)" function where it can be sent to a databse or other service
- This example will simply print the CMX data to the console.
Default port: 5000
Cisco Meraki CMX Documentation
https://documentation.meraki.com/MR/Monitoring_and_Reporting/CMX_Analytics#CMX_Location_API
Written by Cory Guynn
2016
Updated (2020)
www.InternetOfLEGO.com
Meraki.io
"""
# Libraries
from pprint import pprint
from flask import Flask
from flask import json
from flask import request
import sys, getopt
############## USER DEFINED SETTINGS ###############
# MERAKI SETTINGS
validator = "EnterYourValidator"
secret = "EnterYourSecret"
# Save CMX Data
def save_data(data):
print("---- SAVING Location DATA ----")
# CHANGE ME - send 'data' to a database or storage system
pprint(data, indent=1)
####################################################
app = Flask(__name__)
# Respond to Meraki with validator
@app.route('/', methods=['GET'])
def get_validator():
print("validator sent to: ",request.environ['REMOTE_ADDR'])
return validator
# Accept CMX JSON POST
@app.route('/', methods=['POST'])
def get_cmxJSON():
if not request.json or not 'data' in request.json:
return("invalid data",400)
cmxdata = request.json
#pprint(cmxdata, indent=1)
print("Received POST from ",request.environ['REMOTE_ADDR'])
# Verify secret
if cmxdata['secret'] != secret:
print("secret invalid:", cmxdata['secret'])
return("invalid secret",403)
else:
print("secret verified: ", cmxdata['secret'])
# Do something with data (commit to database)
save_data(cmxdata)
# Return success message
return "Location Scanning POST Received"
# Launch application with supplied arguments
def main(argv):
global validator
global secret
try:
opts, args = getopt.getopt(argv,"hv:s:",["validator=","secret="])
except getopt.GetoptError:
print ('cmxreceiver.py -v <validator> -s <secret>')
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print ('cmxreceiver.py -v <validator> -s <secret>')
sys.exit()
elif opt in ("-v", "--validator"):
validator = arg
elif opt in ("-s", "--secret"):
secret = arg
print ('validator: '+validator)
print ('secret: '+secret)
if __name__ == '__main__':
main(sys.argv[1:])
app.run(port=5000,debug=False)