-
Notifications
You must be signed in to change notification settings - Fork 9
/
meraki_dashboard_link_parser.py
290 lines (233 loc) · 11.9 KB
/
meraki_dashboard_link_parser.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
'''
This module is specifically for Meraki Dashboard related operations. This code is responsible for screen scraping
the HTTP Dashboard interface to get the necessary data. It is an optional step, and what it provides is the
ability to cross-launch directly to a device, network, or client from the bot.
'''
import cico_meraki
import requests
import os
import json
import sys
import urllib
import time
# ========================================================
# Load required parameters from environment variables
# ========================================================
meraki_http_un = os.getenv("MERAKI_HTTP_USERNAME")
meraki_http_pw = os.getenv("MERAKI_HTTP_PASSWORD")
meraki_api_token = os.getenv("MERAKI_API_TOKEN")
meraki_org = os.getenv("MERAKI_ORG")
if not meraki_org:
meraki_org = cico_meraki.get_meraki_one_org()
if not meraki_http_un or not meraki_http_pw or not meraki_api_token or not meraki_org:
print("meraki_dashboard_link_parser.py - Missing Environment Variable.")
if not meraki_http_un:
print("MERAKI_HTTP_USERNAME")
if not meraki_http_pw:
print("MERAKI_HTTP_PASSWORD")
if not meraki_api_token:
print("MERAKI_API_TOKEN")
if not meraki_org:
print("MERAKI_ORG")
header = {"X-Cisco-Meraki-API-Key": meraki_api_token}
# ========================================================
# Initialize Program - Function Definitions
# ========================================================
def meraki_www_get_token(strcontent):
'''
When logging into the dashboard, there is an "authenticity_token" hidden field. If this field is not included in
the login POST, login will fail. Scrape this from the HTML and return so it can be POST'ed to login.
:param strcontent: String. Raw HTML of base login page.
:return: String. Authenticity Token.
'''
tokenident = '<input name="authenticity_token" type="hidden" value="'
tokenstart = strcontent.find(tokenident)
tokenval = strcontent[tokenstart + len(tokenident):]
tokenend = tokenval.find('" />')
tokenval = tokenval[:tokenend]
return tokenval
def meraki_www_get_settings(strcontent, settingval, settingfull):
'''
In the dashboard, there are a large number of settings hidden in the code. This function will parse and retrieve
the value of a specific setting
:param strcontent: String. Raw HTML of the page.
:param settingval: String. If the setting is in the "Mkiconf.setting = " format, pass "setting" here. Otherwise...
:param settingfull: String. If the setting is named or punctuated differently, pass the exact string here.
:return: String. The value of the requested setting
'''
# Determine whether this is a standard setting or not, and set tokenident to the exact name of the setting
if settingfull != "":
tokenident = settingfull
else:
tokenident = 'Mkiconf.' + settingval + ' = "'
tokenstart = strcontent.find(tokenident)
if tokenstart < 0:
return ""
tokenval = strcontent[tokenstart + len(tokenident):]
if settingfull == "":
tokenend = tokenval.find('";')
else:
tokenend = tokenval.find(';')
tokenval = tokenval[:tokenend]
return tokenval
def meraki_www_get_path(devtype, devid):
'''
In the dashboard HTTP interface, different device paths have different URL construction. This function will parse
the device type in order to generate the appropriate URL construction.
:param devtype: String. Device type. eg, switch, wireless
:param devid: String. Device ID that will be part of the path.
:return: String. Relevant constructed URL.
'''
if devid != "":
newdevid = "/" + devid
else:
newdevid = ""
retparm = "/manage/nodes/new_list" + newdevid
if devtype == "switch":
retparm = "/manage/nodes/new_list" + newdevid
if devtype == "wired":
retparm = "/manage/nodes/new_wired_status"
if devtype == "camera":
retparm = "/manage/nodes/new_list" + newdevid
if devtype == "wireless":
retparm = "/manage/nodes/new_list" + newdevid
if devtype == "systems_manager":
retparm = "/manage/pcc/list" + newdevid
return retparm
def meraki_www_get_org_list(strcontent):
'''
When logging in, there is a page that lists the organizations that you have access to. This function will parse
this page and return a dictionary of the organizations.
:param strcontent: String. Raw HTML content of the org selection page
:return: Dictionary. All of the organizations including id and name.
'''
orgident = '<a href="/login/org_choose?eid='
orgarr = strcontent.split(orgident)
retarr = {}
for x in range(1, len(orgarr)):
orgend = orgarr[x].find('</a>')
orgdata = orgarr[x][:orgend]
orgdarr = orgdata.split('">')
xst = str(x)
retarr[xst] = {"id": orgdarr[0], "name": orgdarr[1]}
return json.dumps(retarr)
def get_meraki_org_name():
'''
This is an API call that will get a list of all organizations that the API key has access to. It will then search
the list of organizations for the ID matching the orgid that was specified/derived for the bot, and then return
the name of that organization.
:return: String. The organization name for the specified/derived organization.
'''
# Get a list of all networks associated with the specified organization
orgname = ""
url = "https://dashboard.meraki.com/api/v0/organizations"
netlist = requests.get(url, headers=header)
netjson = json.loads(netlist.content.decode("utf-8"))
for n in netjson:
if str(n["id"]) == str(meraki_org):
orgname = n["name"]
return orgname
def get_meraki_org_url(pagecontent):
'''
This function will take the Dictionary of organizations coming from meraki_www_get_org_list, and search that for
the name of the organization from get_meraki_org_name. When it has found a match, it will return the full URL
for that organization.
:param pagecontent: String. Raw HTML of the organization page.
:return: String. URL of the organization to load.
'''
orgurl = ""
olist = json.loads(meraki_www_get_org_list(pagecontent))
org_name_find = get_meraki_org_name()
for onum in olist:
if olist[onum]["name"] == org_name_find:
orgurl = "https://dashboard.meraki.com/login/org_choose?eid=" + olist[onum]["id"]
return orgurl
def get_meraki_api_info():
'''
Unused?
:return:
'''
# Get a list of all networks associated with the specified organization
netjson = cico_meraki.get_meraki_networks()
# Parse list of networks to extract/create URLs needed to get list of devices
urlnet = cico_meraki.collect_url_list(netjson, "https://dashboard.meraki.com/api/v0/networks/$1/devices", "id", "", "", "")
# Get a list of all devices associated with the networks associated to the organization
netlist = cico_meraki.do_multi_get(urlnet, netjson, "id", "", -1, "networkId", "devices")
# Get uplink status of devices
urlnetup = cico_meraki.collect_url_list(netlist, "https://dashboard.meraki.com/api/v0/networks/$1/devices/$2/uplink", "info", "id", "devices", "serial")
netlistup = cico_meraki.do_multi_get(urlnetup, netlist, "devices", "serial", 8, "", "uplinks")
# Split network lists up by device type
newnetlist = cico_meraki.do_split_networks(netlistup)
return newnetlist
def get_meraki_http_info():
'''
Main entry point for function. This function handles the login process, the org redirection, parsing the page
content, and returning/parsing the XHR data.
:return: Dictionary. Dict of relevant mapping data to create cross-launch links.
'''
headers = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0'}
loginurl = "https://dashboard.meraki.com/login/login"
session = requests.Session()
# Get initial login page
r = session.get(loginurl, headers=headers)
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
rcontent = r.content.decode("UTF-8")
tokenval = meraki_www_get_token(rcontent)
print("HTTP Token=", tokenval)
# Post Login data
dataval = {'utf8': '✓', 'email': meraki_http_un, 'password': meraki_http_pw, 'authenticity_token': tokenval, 'commit': 'Log+in', 'goto': 'manage'}
r = session.post(loginurl, headers=headers, data=dataval, cookies=cookies)
rcontent = r.content.decode("UTF-8")
cookies = requests.utils.cookiejar_from_dict(requests.utils.dict_from_cookiejar(session.cookies))
# Check to see if the org selection page has loaded. I've heard that this doesn't always happen, but I've not yet
# found a user to test with that does not hit this page.
if rcontent.lower().find("accounts for " + meraki_http_un.lower()):
orgurl = get_meraki_org_url(rcontent)
print(orgurl)
else:
print("No Account Selection found. Unable to proceed.")
sys.exit()
if orgurl:
# Load redirect page
r = session.get(orgurl, headers=headers, cookies=cookies)
rcontent = r.content.decode("UTF-8")
# Parse content to get auth token and base url. auth token is required for XHR requests, and Base URL is...
# possibly the last network loaded when the user last logged out?
xhrtoken = meraki_www_get_settings(rcontent, "authenticity_token", "")
baseurl = meraki_www_get_settings(rcontent, "base_url", "")
# Search the history to get the most recent FQDN so we can link directly to the appropriate shard.
for resp in r.history:
o = urllib.parse.urlparse(resp.url)
mhost = o.netloc
#"https://%2%%3%manage/organization/overview#t=network"
# Load administered orgs XHR Data
#xhrurl = "https://" + mhost + baseurl + "manage/organization/administered_orgs"
xhrurl = "https://" + mhost + baseurl + "manage/organization/org_json?jsonp=jQuery18307230485578098947_" + str(int(time.time() * 1000)) + "&t0=" + str(int(time.time())) + ".000" + "&t1=" + str(int(time.time())) + ".000" + "&primary_load=true&_=" + str(int(time.time() * 1000))
xhrheader = {'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:51.0) Gecko/20100101 Firefox/51.0',"X-CSRF-Token": xhrtoken,"X-Requested-With": "XMLHttpRequest"}
r = session.get(xhrurl, headers=xhrheader, cookies=cookies)
rcontent = r.content.decode("UTF-8")
rjson = json.loads(rcontent[rcontent.find("({")+1:-1])
#print(rjson)
outjson = {}
mbase = rjson["networks"]
outjson = {"networks": {}, "devices": {}}
# Now, we will iterate the data loaded from the XHR request to generate the mapping data that we need.
for jitem in mbase:
# This generates the link to the network
outjson["networks"][mbase[jitem]["name"]] = {"baseurl": "https://" + mhost + "/" + mbase[jitem]["tag"] + "/n/" + mbase[jitem]["eid"] + meraki_www_get_path(mbase[jitem]["type"], "")} #, "id": mbase[jitem]["id"]
# This generates the link to the device
for jdev in rjson["nodes"]:
if rjson["nodes"][jdev]["ng_id"] == mbase[jitem]["id"]:
outjson["devices"][rjson["nodes"][jdev]["mac"]] = {"baseurl": "https://" + mhost + "/" + mbase[jitem]["tag"] + "/n/" + mbase[jitem]["eid"] + meraki_www_get_path(mbase[jitem]["type"], rjson["nodes"][jdev]["id"]), "desc": rjson["nodes"][jdev]["name"]} #mbase[jitem]["id"] #rjson["nodes"][jdev]["serial"]
return outjson
else:
print("Unable to get org url. Check username and password...")
return {}
#dbjson = get_meraki_http_info()
#print(json.dumps(dbjson))
#os.environ["MERAKI_DASHBOARD_MAP"] = str(dbjson)
#print(str(dbjson))
#print("=======")
#print("MERAKI_DASHBOARD_MAP environment variable set.")
#for n in sorted(get_meraki_api_info()):
# print(n)