-
Notifications
You must be signed in to change notification settings - Fork 198
/
checksubnets.py
366 lines (309 loc) · 14.5 KB
/
checksubnets.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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# This is a script to send an email alert if APs are in invalid management subnets. The alert is
# sent using a SMTP server; by default Gmail. Use an automation platform like Zapier to read this email
# and trigger further actions.
#
# You will need Python 3 and the Requests module installed to run this script:
# https://www.python.org/downloads/
# http://docs.python-requests.org/en/master/user/install/#install
#
# To run the script, enter:
# python checksubnets.py -k <key> -v <valid subs> [-u <user> -p <pass> -d <dest>] [-s <srv> -o <org>]
#
# Mandatory arguments:
# -k <key> : Your Meraki Dashboard API key
# -v <valid subs> : List of valid management subnets. Alert if management IP is not in these
# Arguments to enable sending emails. All three must be given to send email. If omitted, print to screen:
# -u <user> : The username (email address) that will be used to send the alert message
# -p <pass> : Password for the email address where the message is sent from
# -d <dest> : Recipient email address
# Optional arguments:
# -s <server> : Server to use for sending SMTP. If omitted, Gmail will be used
# -o <org> : Name of organization to be processed. If omitted, will process all organizations
#
# Defining subnets:
# Parameter "-v <valid subs>" can take subnets in the following forms:
# <subnet>/<mask>,<subnet>/<mask>,<subnet>/<mask> Example: -v 192.168.128.0/24,10.10.0.0/16
# file:<filename> Example: -v file:validsubs.txt
# <filename> is the name of a text file that defines the valid subnets. Enter one subnet per line
# in the form <subnet>/<mask>
#
# To make script chaining easier, all lines containing informational messages to the user
# start with the character @
import sys, getopt, requests, json, time, ipaddress, smtplib
class c_organizationdata:
def __init__(self):
self.name = ''
self.id = ''
self.shardhost = ''
#end class
#Used for time.sleep(API_EXEC_DELAY). Delay added to avoid hitting dashboard API max request rate
API_EXEC_DELAY = 0.21
def printusertext(p_message):
#prints a line of text that is meant for the user to read
#do not process these lines when chaining scripts
print('@ %s' % p_message)
def printhelp():
#prints help text
printusertext('This is a script to send an email alert if APs are in invalid management subnets. The alert is')
printusertext(' sent using a SMTP server; by default Gmail. Use an automation platform like Zapier to read this email')
printusertext(' and trigger further actions.')
printusertext('')
printusertext('To run the script, enter:')
printusertext(' python checksubnets.py -k <key> -v <valid subs> [-u <user> -p <pass> -d <dest>] [-s <srv> -o <org>]')
printusertext('')
printusertext('Mandatory arguments:')
printusertext(' -k <key> : Your Meraki Dashboard API key')
printusertext(' -v <valid subs> : List of valid management subnets. Alert if management IP is not in these')
printusertext('Arguments to enable sending emails. All three must be given to send email. If omitted, print to screen:')
printusertext(' -u <user> : The username (email address) that will be used to send the alert message')
printusertext(' -p <pass> : Password for the email address where the message is sent from')
printusertext(' -d <dest> : Recipient email address')
printusertext('Optional arguments:')
printusertext(' -s <server> : Server to use for sending SMTP. If omitted, Gmail will be used')
printusertext(' -o <org> : Name of organization to be processed. If omitted, will process all organizations')
printusertext('')
printusertext('Defining subnets:')
printusertext(' Parameter "-v <valid subs>" can take subnets in the following forms:')
printusertext(' <subnet>/<mask>,<subnet>/<mask>,<subnet>/<mask> Example: -v 192.168.128.0/24,10.10.0.0/16')
printusertext(' file:<filename> Example: -v file:validsubs.txt')
printusertext(' <filename> is the name of a text file that defines the valid subnets. Enter one subnet per line')
printusertext(' in the form <subnet>/<mask>')
printusertext('')
printusertext('To make script chaining easier, all lines containing informational messages to the user')
printusertext(' start with the character @')
printusertext('')
printusertext('In Windows, use double quotes ("") to pass arguments containing spaces.')
def getorglist(p_apikey):
#returns the organizations' list for a specified admin
time.sleep(API_EXEC_DELAY)
try:
r = requests.get('https://dashboard.meraki.com/api/v0/organizations', headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
except:
printusertext('ERROR 01: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'id':'null'})
return returnvalue
rjson = r.json()
return(rjson)
def getshardhost(p_apikey, p_orgid):
#quick-n-dirty patch
return("api.meraki.com")
def getnwlist(p_apikey, p_shardhost, p_orgid):
#returns a list of all networks in an organization
#on failure returns a single record with 'null' name and id
time.sleep(API_EXEC_DELAY)
try:
r = requests.get('https://%s/api/v0/organizations/%s/networks' % (p_shardhost, p_orgid), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
except:
printusertext('ERROR 03: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'name': 'null', 'id': 'null'})
return(returnvalue)
return(r.json())
def getdevicelist(p_apikey, p_shardhost, p_nwid):
#returns a list of all devices in a network
time.sleep(API_EXEC_DELAY)
try:
r = requests.get('https://%s/api/v0/networks/%s/devices' % (p_shardhost, p_nwid), headers={'X-Cisco-Meraki-API-Key': p_apikey, 'Content-Type': 'application/json'})
except:
printusertext('ERROR 04: Unable to contact Meraki cloud')
sys.exit(2)
returnvalue = []
if r.status_code != requests.codes.ok:
returnvalue.append({'serial': 'null', 'model': 'null'})
return(returnvalue)
return(r.json())
def loadsubsfile(p_filename):
# load subnets' definition file
returnstr = ''
try:
f = open(p_filename, 'r')
except:
printusertext('ERROR 05: Unable to open file "%s"' % p_filename)
sys.exit(2)
for line in f:
stripped = line.strip()
#drop blank lines
if len(stripped) > 0:
#drop comments
if stripped[0] != '#':
if len(returnstr) != 0:
returnstr += ','
returnstr += stripped
f.close()
return (returnstr)
def main(argv):
# python checksubnets.py -k <key> -v <valid subs> [-u <user> -p <pass> -d <dest>] [-s <srv> -o <org>]
#initialize variables for command line arguments
arg_apikey = ''
arg_subs = ''
arg_user = ''
arg_pass = ''
arg_dest = ''
arg_server = ''
arg_org = ''
#get command line arguments
try:
opts, args = getopt.getopt(argv, 'hk:v:u:p:d:s:o:')
except getopt.GetoptError:
printhelp()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
printhelp()
sys.exit()
elif opt == '-k':
arg_apikey = arg
elif opt == '-v':
arg_subs = arg
elif opt == '-u':
arg_user = arg
elif opt == '-p':
arg_pass = arg
elif opt == '-d':
arg_dest = arg
elif opt == '-s':
arg_server = arg
elif opt == '-o':
arg_org = arg
#check if all required parameters have been given
if arg_apikey == '' or arg_subs == '':
printhelp()
sys.exit(2)
#set flags and default parameters
#NOTE: EDIT THESE LINES TO MODIFY DEFAULT BEHAVIOR
emailoptcount = 0
if arg_user != '':
emailoptcount += 1
if arg_pass != '':
emailoptcount += 1
if arg_dest != '':
emailoptcount += 1
if 0 < emailoptcount < 3:
printusertext('ERROR 06: Source account, password and destination must be given to send email report')
sys.exit(2)
if arg_server == '':
arg_server = 'smtp.gmail.com:587'
#check if subs is file or list. load contents
splitsubs = arg_subs.split(':')
subsbuffer = ''
if len(splitsubs) > 1:
if(splitsubs[0].strip()) == 'file':
subsbuffer = loadsubsfile(splitsubs[1].strip())
else:
printusertext('ERROR 07: Invalid format for subnet definitions')
sys.exit(2)
else:
subsbuffer = arg_subs.strip()
#create subnets' list
subnetlist = []
splitbuffer = subsbuffer.split(',')
for item in splitbuffer:
try:
subnetlist.append(ipaddress.IPv4Network(item.strip()))
except:
printusertext('ERROR 08: Invalid format for subnet definitions')
sys.exit(2)
printusertext('INFO: Retrieving organization info')
orgs = []
#compile list of organizations to be processed
orgjson = getorglist(arg_apikey)
if orgjson[0]['id'] == 'null':
printusertext('ERROR 09: Unable to retrieve org list')
sys.exit(2)
i = 0
for record in orgjson:
if arg_org == '' or record['name'] == arg_org:
orgs.append(c_organizationdata())
orgs[i].name = record['name']
orgs[i].id = record['id']
i += 1
#get shard host/FQDN where destination org is stored
#this call sometimes fails. implementing a try-verify-wait-repeat loop
MAX_SHARD_RESOLVE_TRIES = 10
for record in orgs:
flag_unabletoresolveshard = True
for i in range (0, MAX_SHARD_RESOLVE_TRIES):
shardhost = getshardhost(arg_apikey, record.id)
if shardhost == 'null':
time.sleep(API_EXEC_DELAY*(i+1))
else:
flag_unabletoresolveshard = False
break
if flag_unabletoresolveshard:
printusertext('ERROR 10: Unable to read data for org "%s"' % record.name)
sys.exit(2)
else:
record.shardhost = shardhost
#compile list of incompliant devices
outputstr = 'Devices not in subnets "%s":\r\n' % subsbuffer
flag_gotdevice = False
for org in orgs:
flag_neworg = True
netbuffer = getnwlist(arg_apikey, org.shardhost, org.id)
if len(netbuffer) > 0:
if netbuffer[0]['id'] != 'null':
for net in netbuffer:
flag_newnet = True
devbuffer = getdevicelist(arg_apikey, org.shardhost, net['id'])
if len(devbuffer) > 0:
if devbuffer[0]['serial'] != 'null':
for dev in devbuffer:
if dev['model'][:2] == 'MR':
foundsub = False
if not dev['lanIp'] is None:
for sub in subnetlist:
if ipaddress.IPv4Address(dev['lanIp']) in sub:
foundsub = True
break
if not foundsub:
if flag_neworg:
outputstr += '\r\n---\r\n\r\nOrganization: "%s"\r\n' % org.name
flag_neworg = False
if flag_newnet:
outputstr += '\r\nNetwork: "%s"\r\n' % net['name']
flag_newnet = False
outputstr += '%s %s\r\n' % (dev['serial'], dev['lanIp'])
flag_gotdevice = True
else:
printusertext('WARNING: Unable to read device data for network "%s"' % net['name'])
else:
printusertext('INFO: Network "%s" contains no devices' % net['name'])
else:
printusertext('WARNING: Unable to read network data for org "%s"' % org.name)
else:
printusertext('INFO: Organization "%s" contains no networks' % org.name)
#print output or send email if there is something to report
if flag_gotdevice:
if arg_user != '':
fromaddr = arg_user
toaddrs = arg_dest
msg = "\r\n".join([
"From: %s" % fromaddr,
"To: %s" % toaddrs,
"Subject: Meraki access points with incompliant management IPs",
"", outputstr])
username = arg_user
password = arg_pass
try:
server = smtplib.SMTP(arg_server)
server.ehlo()
server.starttls()
server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()
except:
printusertext('ERROR 11: Unable to send email')
sys.exit(2)
printusertext('INFO: Email sent to %s' % toaddrs)
else:
print(outputstr)
else:
printusertext('INFO: All devices found are in scope "%s"' % subsbuffer)
printusertext('INFO: End of script.')
if __name__ == '__main__':
main(sys.argv[1:])