-
Notifications
You must be signed in to change notification settings - Fork 198
/
export_mx_l3.py
87 lines (67 loc) · 2.51 KB
/
export_mx_l3.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
#!/usr/bin/python3
READ_ME = '''
=== PREREQUISITES ===
Run in Python 3 with Meraki dashboard API Python library @
https://github.com/meraki/dashboard-api-python/
pip[3] install --upgrade meraki
=== DESCRIPTION ===
Exports CSV of MX L3 outbound firewall rules.
=== USAGE ===
python[3] export_mx_l3.py [-k <api_key>] -n <net_id>
API key can also be exported as an environment variable named
MERAKI_DASHBOARD_API_KEY
'''
import csv
from datetime import datetime
import getopt
import os
import sys
import meraki
# Prints READ_ME help message for user to read
def print_help():
lines = READ_ME.split('\n')
for line in lines:
print('# {0}'.format(line))
def main(argv):
# Set default values for command line arguments
api_key = net_id = None
# Get command line arguments
try:
opts, args = getopt.getopt(argv, 'hk:n:')
except getopt.GetoptError:
print_help()
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print_help()
sys.exit()
elif opt == '-k':
api_key = arg
elif opt == '-n':
net_id = arg
# Check if all required parameters have been input
if (api_key == None and os.getenv('MERAKI_DASHBOARD_API_KEY') == None) or net_id == None:
print_help()
sys.exit(2)
# Set the CSV output file and write the header row
time_now = f'{datetime.now():%Y-%m-%d_%H-%M-%S}'
file_name = f'mx_l3fw_rules__{time_now}.csv'
output_file = open(file_name, mode='w', newline='\n')
field_names = ['policy','protocol','srcCidr','srcPort','destCidr','destPort','comment','logging']
csv_writer = csv.writer(output_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
csv_writer.writerow(field_names)
# Dashboard API library class
m = meraki.DashboardAPI(api_key=api_key, log_file_prefix=__file__[:-3])
# Read configuration of MX L3 firewall rules
fw_rules = m.appliance.getNetworkApplianceFirewallL3FirewallRules(net_id)
if 'rules' in fw_rules and len(fw_rules['rules']) > 0:
# Loop through each firewall rule and write to CSV
for rule in fw_rules['rules']:
csv_row = [rule['policy'], rule['protocol'], rule['srcCidr'], rule['srcPort'], rule['destCidr'], rule['destPort'], rule['comment'], rule['syslogEnabled']]
csv_writer.writerow(csv_row)
output_file.close()
print(f'Export completed to file {file_name}')
else:
print(f'No firewall rules to export')
if __name__ == '__main__':
main(sys.argv[1:])