-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerakibackup
executable file
·361 lines (282 loc) · 14.8 KB
/
merakibackup
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
#!/usr/bin/env python3
"""
merakibackup.py
Iterates through all organizations and networks visable to the APIKEY,
retreives data from all known API endpoints and then outputs as
a single JSON array.
Before running the script, it's best to put the APIKEY
into the MERAKI_DASHBOARD_API_KEY environment variable. Otherwise
use the less security --apikey option.
export MERAKI_DASHBOARD_API_KEY="the_api_key_here"
curl -s -L -H "X-Cisco-Meraki-API-Key: $MERAKI_DASHBOARD_API_KEY" -X GET \
-H 'Content-Type: application/json' 'https://dashboard.meraki.com/api/v0/organizations' | jq '.'
"""
import getopt
import json
import os
import re
import sys
import meraki
class MerakiBackup:
"""
Create a backup JSON of a Meraki dashboard
"""
def __init__(self, verbose=False):
self.configuration = {}
self.verbose = verbose
self.debug = False
self.apikey = None
self.output_filename = None
self.dashboard = None
def getNetworkApplianceConnectivityMonitoringDestinations(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceConnectivityMonitoringDestinations(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceContentFiltering(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceContentFiltering(networkId)
except meraki.exceptions.APIError:
return None
def getDeviceLldpCdp(self, networkId: str, serial: str) -> dict:
try:
return self.dashboard.devices.getDeviceLldpCdp(serial)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallFirewalledServices(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallFirewalledServices(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkGroupPolicies(self, networkId: str) -> dict:
try:
return self.dashboard.networks.getNetworkGroupPolicies(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceSecurityIntrusion(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceSecurityIntrusion(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceSecurityMalware(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceSecurityMalware(networkId)
except meraki.exceptions.APIError:
return None
def getDeviceManagementInterface(self, networkId: str, serial: str) -> dict:
try:
return self.dashboard.devices.getDeviceManagementInterface(serial)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallOneToOneNatRules(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallOneToOneNatRules(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallOneToManyNatRules(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallOneToManyNatRules(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallL3FirewallRules(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallL3FirewallRules(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallL7FirewallRules(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallL7FirewallRules(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallL7FirewallRulesApplicationCategories(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallL7FirewallRulesApplicationCategories(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceFirewallPortForwardingRules(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceFirewallPortForwardingRules(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceStaticRoutes(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceStaticRoutes(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkAppliancePorts(self, networkId: str):
try:
return self.dashboard.appliance.getNetworkAppliancePorts(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceWarmSpare(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceWarmSpare(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceVpnSiteToSiteVpn(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceVpnSiteToSiteVpn(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkSnmp(self, networkId: str) -> dict:
try:
return self.dashboard.networks.getNetworkSnmp(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkWirelessSsids(self, networkId: str) -> dict:
try:
return self.dashboard.wireless.getNetworkWirelessSsids(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkSyslogServers(self, networkId: str) -> dict:
try:
return self.dashboard.networks.getNetworkSyslogServers(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceVlans(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceVlans(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkApplianceVlansSettings(self, networkId: str) -> dict:
try:
return self.dashboard.appliance.getNetworkApplianceVlansSettings(networkId)
except meraki.exceptions.APIError:
return None
def getNetworkWirelessSettings(self, networkId: str) -> dict:
try:
return self.dashboard.wireless.getNetworkWirelessSettings(networkId)
except meraki.exceptions.APIError:
return None
def backup(self, only_organization_id: None, networkFilter: str):
if networkFilter:
filterRe = "^" + networkFilter + ".*"
regex = re.compile(filterRe, flags=re.IGNORECASE)
organizations = self.dashboard.organizations.getOrganizations()
if not organizations:
print("No organizations found")
return
for organization in organizations:
if self.verbose:
#print("org={}".format(organizations))
print("Organization: {} {}".format(organization['id'], organization['name']))
if only_organization_id and only_organization_id != organization['id']:
if self.verbose:
print("skipping")
continue
networks = self.dashboard.organizations.getOrganizationNetworks(organization['id'])
if networks:
for network in networks:
if self.verbose:
print("Network: {}".format(network['name']))
#print(f'debug: network={network}')
if networkFilter and not regex.match(network['name']):
continue
alertSettings = self.dashboard.networks.getNetworkAlertsSettings(network['id'])
network['alertSettings'] = alertSettings
httpServers = self.dashboard.networks.getNetworkWebhooksHttpServers(network['id'])
network['httpServers'] = httpServers
if 'appliance' in network['productTypes']:
network['connectivityMonitoringDestinations'] = self.getNetworkApplianceConnectivityMonitoringDestinations(network['id'])
vlansSettings = self.getNetworkApplianceVlansSettings(network['id'])
network['vlansSettings'] = vlansSettings
if vlansSettings and vlansSettings['vlansEnabled']:
network['vlans'] = self.getNetworkApplianceVlans(network['id'])
network['appliancePorts'] = self.getNetworkAppliancePorts(network['id'])
network['firewalledServices'] = self.getNetworkApplianceFirewallFirewalledServices(network['id'])
network['staticRoutes'] = self.getNetworkApplianceStaticRoutes(network['id'])
network['siteToSiteVpn'] = self.getNetworkApplianceVpnSiteToSiteVpn(network['id'])
network['l3FirewallRules'] = self.getNetworkApplianceFirewallL3FirewallRules(network['id'])
# should figure out if MX
network['intrusionSettings'] = self.getNetworkApplianceSecurityIntrusion(network['id'])
network['malwareSettings'] = self.getNetworkApplianceSecurityMalware(network['id'])
network['warmSpareSettings'] = self.getNetworkApplianceWarmSpare(network['id'])
network['l7FirewallRules'] = self.getNetworkApplianceFirewallL7FirewallRules(network['id'])
network['l7ApplicationCategories'] = self.getNetworkApplianceFirewallL7FirewallRulesApplicationCategories(network['id'])
network['contentFiltering'] = self.getNetworkApplianceContentFiltering(network['id'])
network['oneToOneNatRules'] = self.getNetworkApplianceFirewallOneToOneNatRules(network['id'])
network['oneToManyNatRules'] = self.getNetworkApplianceFirewallOneToManyNatRules(network['id'])
network['portForwardingRules'] = self.getNetworkApplianceFirewallPortForwardingRules(network['id'])
network['groupPolicies'] = self.getNetworkGroupPolicies(network['id'])
network['syslogServers'] = self.getNetworkSyslogServers(network['id'])
network['snmp'] = self.getNetworkSnmp(network['id'])
if 'wireless' in network['productTypes']:
network['wirelessSsids'] = self.getNetworkWirelessSsids(network['id'])
network['wirelessSettings'] = self.getNetworkWirelessSettings(network['id'])
devices = self.dashboard.networks.getNetworkDevices(network['id'])
network['devices'] = devices
if devices:
for device in devices:
device_name = None
if 'name' in device:
device_name = device['name']
#print(f'debug: device={device}')
if self.verbose:
print(" Device: {}".format(device_name))
if device['model'].startswith('MX'):
uplink = self.dashboard.appliance.getDeviceApplianceUplinksSettings(device['serial'])
device['uplink'] = uplink
lldp_cdp = self.getDeviceLldpCdp(network['id'], device['serial'])
device['lldp_cdp'] = lldp_cdp
if device['model'].startswith('MX') or device['model'].startswith('MR'):
device['managementInterfaceSettings'] = self.getDeviceManagementInterface(network['id'], device['serial'])
organization['networks'] = networks
admins = self.dashboard.organizations.getOrganizationAdmins(organization['id'])
organization['admins'] = admins
vpnFirewallRules = self.dashboard.appliance.getOrganizationApplianceVpnVpnFirewallRules(organization['id'])
organization['vpnFirewallRules'] = vpnFirewallRules
thirdPartyVPNPeers = self.dashboard.appliance.getOrganizationApplianceVpnThirdPartyVPNPeers(organization['id'])
organization['thirdPartyVPNPeers'] = thirdPartyVPNPeers
samlRoles = self.dashboard.organizations.getOrganizationSamlRoles(organization['id'])
organization['samlRoles'] = samlRoles
if self.output_filename:
file = open(self.output_filename, "w")
file.write(json.dumps(organizations, indent=2, sort_keys=True))
file.close()
if self.verbose:
print("Wrote output to {}".format(self.output_filename))
else:
print("{}".format(json.dumps(organizations, indent=2, sort_keys=True)))
def main(self):
"""
Parse command line options and perform backup.
"""
organization = None
networkFilter = None
options = getopt.getopt(sys.argv[1:],
'a:f:o:w:v',
[
'apikey=',
'filter=',
'organization=',
'write=',
'verbose',
])[0]
for opt, arg in options:
if opt in ('-w', '--write'):
self.output_filename = arg
elif opt in ('-a', '--apikey'):
self.apikey = arg
elif opt in ('-o', '--organization'):
organization = arg
elif opt in ('-f', '--filter'):
networkFilter = arg
elif opt in ('-v', '--verbose'):
self.verbose = True
if 'MERAKI_APIKEY' in os.environ:
self.apikey = os.environ['MERAKI_APIKEY']
if 'MERAKI_DASHBOARD_API_KEY' in os.environ:
self.apikey = os.environ['MERAKI_DASHBOARD_API_KEY']
if not self.apikey:
print("Missing apikey")
sys.exit(2)
try:
self.dashboard = meraki.DashboardAPI(api_key=self.apikey,
output_log=self.debug,
print_console=self.debug)
except meraki.exceptions.APIKeyError as exception:
print("Error: {}".format(exception))
return
self.backup(organization, networkFilter)
if __name__ == "__main__":
MerakiBackup().main()