-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathlocal_subnet_dumper.py
More file actions
136 lines (106 loc) · 3.94 KB
/
Copy pathlocal_subnet_dumper.py
File metadata and controls
136 lines (106 loc) · 3.94 KB
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
import json
import meraki
"""
2023-11-13
Author: John M. Kuchta ( TKIPisalegacycipher // https://github.com/TKIPisalegacycipher )
Requrires: Meraki library v1.39.0 or later.
This script gathers all your appliances' local subnets from your organizations' networks and dumps them to a JSON file.
You might find this handy for certain IPAM exercises. Subnets from appliances in "single LAN" mode will have VLAN 0.
EXTRA CREDIT
If you are feeling adventurous, or just want an opportunity to flex your Python skills, consider re-writing this script
to work asynchronously, which can substantially improve the speed for large environments. You might start by gathering
all the relevant information asynchronously, then doing the list comprehensions after the API calls are complete.
"""
# You can exclude specific organization or networks here. This is optional but recommended if you have a large
# deployment including lots of irrelevant networks.
excluded_org_ids = []
excluded_network_ids = []
# init session
d = meraki.DashboardAPI()
# gather orgs
my_orgs = d.organizations.getOrganizations()
my_orgs = [org for org in my_orgs if org["id"] not in excluded_org_ids]
print(f"done gathering organizations")
# gather networks
my_networks = [
d.organizations.getOrganizationNetworks(
organization["id"], total_pages=all
)
for organization in my_orgs
]
print(f"done gathering networks")
my_appliance_networks = [
network
for netlist in my_networks
for network in netlist
if network["id"] not in excluded_network_ids
and "appliance" in network["productTypes"]
]
print(f"done gathering appliance networks")
# gather routed networks -- appliances in passthrough mode don't have local subnets
my_appliance_routed_networks = [
network
for network in my_appliance_networks
if d.appliance.getNetworkApplianceSettings(network["id"])["deploymentMode"]
== "routed"
]
print(f"done gathering routed appliance networks")
my_appliance_networks_with_vlans = [
network
for network in my_appliance_routed_networks
if d.appliance.getNetworkApplianceVlansSettings(network["id"])["vlansEnabled"]
]
print(f"done gathering appliance network vlan settings")
my_appliance_networks_without_vlans = [
network
for network in my_appliance_routed_networks
if network not in my_appliance_networks_with_vlans
]
my_vlan_lists = [
{
"organizationId": network["organizationId"],
"networkId": network["id"],
"vlans": d.appliance.getNetworkApplianceVlans(network["id"]),
}
for network in my_appliance_networks_with_vlans
]
print(f"done gathering appliance network vlans")
my_lans = [
{
"organizationId": network["organizationId"],
"networkId": network["id"],
"lan": d.appliance.getNetworkApplianceSingleLan(network["id"]),
}
for network in my_appliance_networks_without_vlans
]
print(f"done gathering appliance network lans")
# unpack the subnets
vlan_subnets = list()
for item in my_vlan_lists:
for vlan in item["vlans"]:
this_subnet = dict()
this_subnet["organizationId"] = item["organizationId"]
this_subnet["networkId"] = vlan["networkId"]
this_subnet["subnet"] = vlan["subnet"]
this_subnet["vlanId"] = vlan["id"]
this_subnet["applianceIp"] = vlan["applianceIp"]
vlan_subnets.append(this_subnet)
lan_subnets = list()
for item in my_lans:
this_subnet = dict()
this_subnet["organizationId"] = item["organizationId"]
this_subnet["networkId"] = item["networkId"]
this_subnet["subnet"] = item["lan"]["subnet"]
this_subnet["vlanId"] = 0
this_subnet["applianceIp"] = item["lan"]["applianceIp"]
lan_subnets.append(this_subnet)
all_subnets = vlan_subnets + lan_subnets
print("done assembling subnets")
# dump the subnets to a JSON file
json_object = json.dumps(all_subnets, indent=4)
with open(
"subnets.json",
"w",
) as outfile:
outfile.write(json_object)
print("subnets dumped to subnets.json")