Skip to content

Commit 5e98ce0

Browse files
Create local_subnet_dumper.py
New example script for IPAM.
1 parent 7e587ea commit 5e98ce0

1 file changed

Lines changed: 136 additions & 0 deletions

File tree

examples/local_subnet_dumper.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
import json
2+
3+
import meraki
4+
5+
"""
6+
2023-11-13
7+
Author: John M. Kuchta ( TKIPisalegacycipher // https://github.com/TKIPisalegacycipher )
8+
Requrires: Meraki library v1.39.0 or later.
9+
10+
This script gathers all your appliances' local subnets from your organizations' networks and dumps them to a JSON file.
11+
You might find this handy for certain IPAM exercises. Subnets from appliances in "single LAN" mode will have VLAN 0.
12+
13+
EXTRA CREDIT
14+
If you are feeling adventurous, or just want an opportunity to flex your Python skills, consider re-writing this script
15+
to work asynchronously, which can substantially improve the speed for large environments. You might start by gathering
16+
all the relevant information asynchronously, then doing the list comprehensions after the API calls are complete.
17+
"""
18+
19+
# You can exclude specific organization or networks here. This is optional but recommended if you have a large
20+
# deployment including lots of irrelevant networks.
21+
excluded_org_ids = []
22+
excluded_network_ids = []
23+
24+
# init session
25+
d = meraki.DashboardAPI()
26+
27+
# gather orgs
28+
my_orgs = d.organizations.getOrganizations()
29+
my_orgs = [org for org in my_orgs if org["id"] not in excluded_org_ids]
30+
31+
print(f"done gathering organizations")
32+
33+
# gather networks
34+
my_networks = [
35+
d.organizations.getOrganizationNetworks(
36+
organization["id"], total_pages=all
37+
)
38+
for organization in my_orgs
39+
]
40+
41+
print(f"done gathering networks")
42+
43+
my_appliance_networks = [
44+
network
45+
for netlist in my_networks
46+
for network in netlist
47+
if network["id"] not in excluded_network_ids
48+
and "appliance" in network["productTypes"]
49+
]
50+
51+
print(f"done gathering appliance networks")
52+
53+
# gather routed networks -- appliances in passthrough mode don't have local subnets
54+
my_appliance_routed_networks = [
55+
network
56+
for network in my_appliance_networks
57+
if d.appliance.getNetworkApplianceSettings(network["id"])["deploymentMode"]
58+
== "routed"
59+
]
60+
61+
print(f"done gathering routed appliance networks")
62+
63+
my_appliance_networks_with_vlans = [
64+
network
65+
for network in my_appliance_routed_networks
66+
if d.appliance.getNetworkApplianceVlansSettings(network["id"])["vlansEnabled"]
67+
]
68+
69+
print(f"done gathering appliance network vlan settings")
70+
71+
my_appliance_networks_without_vlans = [
72+
network
73+
for network in my_appliance_routed_networks
74+
if network not in my_appliance_networks_with_vlans
75+
]
76+
77+
my_vlan_lists = [
78+
{
79+
"organizationId": network["organizationId"],
80+
"networkId": network["id"],
81+
"vlans": d.appliance.getNetworkApplianceVlans(network["id"]),
82+
}
83+
for network in my_appliance_networks_with_vlans
84+
]
85+
86+
print(f"done gathering appliance network vlans")
87+
88+
my_lans = [
89+
{
90+
"organizationId": network["organizationId"],
91+
"networkId": network["id"],
92+
"lan": d.appliance.getNetworkApplianceSingleLan(network["id"]),
93+
}
94+
for network in my_appliance_networks_without_vlans
95+
]
96+
97+
print(f"done gathering appliance network lans")
98+
99+
# unpack the subnets
100+
vlan_subnets = list()
101+
102+
for item in my_vlan_lists:
103+
for vlan in item["vlans"]:
104+
this_subnet = dict()
105+
this_subnet["organizationId"] = item["organizationId"]
106+
this_subnet["networkId"] = vlan["networkId"]
107+
this_subnet["subnet"] = vlan["subnet"]
108+
this_subnet["vlanId"] = vlan["id"]
109+
this_subnet["applianceIp"] = vlan["applianceIp"]
110+
vlan_subnets.append(this_subnet)
111+
112+
lan_subnets = list()
113+
114+
for item in my_lans:
115+
this_subnet = dict()
116+
this_subnet["organizationId"] = item["organizationId"]
117+
this_subnet["networkId"] = item["networkId"]
118+
this_subnet["subnet"] = item["lan"]["subnet"]
119+
this_subnet["vlanId"] = 0
120+
this_subnet["applianceIp"] = item["lan"]["applianceIp"]
121+
lan_subnets.append(this_subnet)
122+
123+
all_subnets = vlan_subnets + lan_subnets
124+
125+
print("done assembling subnets")
126+
127+
# dump the subnets to a JSON file
128+
json_object = json.dumps(all_subnets, indent=4)
129+
130+
with open(
131+
"../../../../../Users/jkuchta/AppData/Roaming/JetBrains/PyCharm2023.2/scratches/subnets.json",
132+
"w",
133+
) as outfile:
134+
outfile.write(json_object)
135+
136+
print("subnets dumped to subnets.json")

0 commit comments

Comments
 (0)