Skip to content

Commit 9f3a0ed

Browse files
Fixes #297.
1 parent a026860 commit 9f3a0ed

File tree

1 file changed

+80
-38
lines changed

1 file changed

+80
-38
lines changed

examples/org_wide_clients_v1.py

Lines changed: 80 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -14,96 +14,138 @@
1414
def main():
1515
# Instantiate a Meraki dashboard API session
1616
dashboard = meraki.DashboardAPI(
17-
api_key='',
18-
base_url='https://api.meraki.com/api/v1/',
17+
api_key="",
18+
base_url="https://api.meraki.com/api/v1/",
1919
output_log=True,
2020
log_file_prefix=os.path.basename(__file__)[:-3],
21-
log_path='',
22-
print_console=False
21+
log_path="",
22+
print_console=False,
2323
)
2424

2525
# Get list of organizations to which API key has access
2626
organizations = dashboard.organizations.getOrganizations()
2727

2828
# Iterate through list of orgs
2929
for org in organizations:
30-
print(f'\nAnalyzing organization {org["name"]}:')
31-
org_id = org['id']
30+
print(f"\nAnalyzing organization {org['name']}:")
31+
org_id = org["id"]
3232

3333
# Get list of networks in organization
3434
try:
3535
networks = dashboard.organizations.getOrganizationNetworks(org_id)
3636
except meraki.APIError as e:
37-
print(f'Meraki API error: {e}')
38-
print(f'status code = {e.status}')
39-
print(f'reason = {e.reason}')
40-
print(f'error = {e.message}')
37+
print(f"Meraki API error: {e}")
38+
print(f"status code = {e.status}")
39+
print(f"reason = {e.reason}")
40+
print(f"error = {e.message}")
4141
continue
4242
except Exception as e:
43-
print(f'some other error: {e}')
43+
print(f"some other error: {e}")
4444
continue
4545

4646
# Create local folder
47-
todays_date = f'{datetime.now():%Y-%m-%d}'
48-
folder_name = f'Org {org_id} clients {todays_date}'
47+
todays_date = f"{datetime.now():%Y-%m-%d}"
48+
folder_name = f"Org {org_id} clients {todays_date}"
4949
if folder_name not in os.listdir():
5050
os.mkdir(folder_name)
5151

5252
# Iterate through networks
5353
total = len(networks)
5454
counter = 1
55-
print(f' - iterating through {total} networks in organization {org_id}')
55+
print(f" - iterating through {total} networks in organization {org_id}")
5656
for net in networks:
57-
print(f'Finding clients in network {net["name"]} ({counter} of {total})')
57+
print(f"Finding clients in network {net['name']} ({counter} of {total})")
5858
try:
5959
# Get list of clients on network, filtering on timespan of last 14 days
60-
clients = dashboard.networks.getNetworkClients(net['id'], timespan=60 * 60 * 24 * 14, perPage=1000,
61-
total_pages='all')
60+
clients = dashboard.networks.getNetworkClients(
61+
net["id"],
62+
timespan=60 * 60 * 24 * 14,
63+
perPage=1000,
64+
total_pages="all",
65+
)
6266
except meraki.APIError as e:
63-
print(f'Meraki API error: {e}')
64-
print(f'status code = {e.status}')
65-
print(f'reason = {e.reason}')
66-
print(f'error = {e.message}')
67+
print(f"Meraki API error: {e}")
68+
print(f"status code = {e.status}")
69+
print(f"reason = {e.reason}")
70+
print(f"error = {e.message}")
6771
except Exception as e:
68-
print(f'some other error: {e}')
72+
print(f"some other error: {e}")
6973
else:
7074
if clients:
7175
# Write to file
72-
file_name = f'{net["name"]}.csv'
73-
output_file = open(f'{folder_name}/{file_name}', mode='w', newline='\n')
76+
file_name = f"{net['name']}.csv"
77+
output_file = open(
78+
f"{folder_name}/{file_name}", mode="w", newline="\n"
79+
)
7480
field_names = clients[0].keys()
75-
csv_writer = csv.DictWriter(output_file, field_names, delimiter=',', quotechar='"',
76-
quoting=csv.QUOTE_ALL)
81+
csv_writer = csv.DictWriter(
82+
output_file,
83+
field_names,
84+
delimiter=",",
85+
quotechar='"',
86+
quoting=csv.QUOTE_ALL,
87+
extrasaction="ignore",
88+
)
7789
csv_writer.writeheader()
7890
csv_writer.writerows(clients)
7991
output_file.close()
80-
print(f' - found {len(clients)}')
92+
print(f" - found {len(clients)}")
8193

8294
counter += 1
8395

8496
# Stitch together one consolidated CSV per org
85-
output_file = open(f'{folder_name}.csv', mode='w', newline='\n')
86-
field_names = ['id', 'mac', 'description', 'ip', 'ip6', 'ip6Local', 'user', 'firstSeen', 'lastSeen',
87-
'manufacturer', 'os', 'recentDeviceSerial', 'recentDeviceName', 'recentDeviceMac', 'ssid',
88-
'vlan', 'switchport', 'usage', 'status', 'notes', 'smInstalled', 'groupPolicy8021x']
97+
output_file = open(f"{folder_name}.csv", mode="w", newline="\n")
98+
field_names = [
99+
"id",
100+
"mac",
101+
"description",
102+
"ip",
103+
"ip6",
104+
"ip6Local",
105+
"user",
106+
"firstSeen",
107+
"lastSeen",
108+
"manufacturer",
109+
"os",
110+
"recentDeviceSerial",
111+
"recentDeviceName",
112+
"recentDeviceMac",
113+
"ssid",
114+
"vlan",
115+
"switchport",
116+
"usage",
117+
"status",
118+
"notes",
119+
"smInstalled",
120+
"groupPolicy8021x",
121+
]
89122
field_names.insert(0, "Network Name")
90123
field_names.insert(1, "Network ID")
91124

92-
csv_writer = csv.DictWriter(output_file, field_names, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
125+
csv_writer = csv.DictWriter(
126+
output_file,
127+
field_names,
128+
delimiter=",",
129+
quotechar='"',
130+
quoting=csv.QUOTE_ALL,
131+
extrasaction="ignore",
132+
)
93133
csv_writer.writeheader()
94134
for net in networks:
95-
file_name = f'{net["name"]}.csv'
135+
file_name = f"{net['name']}.csv"
96136
if file_name in os.listdir(folder_name):
97-
with open(f'{folder_name}/{file_name}') as input_file:
98-
csv_reader = csv.DictReader(input_file, delimiter=',', quotechar='"', quoting=csv.QUOTE_ALL)
137+
with open(f"{folder_name}/{file_name}") as input_file:
138+
csv_reader = csv.DictReader(
139+
input_file, delimiter=",", quotechar='"', quoting=csv.QUOTE_ALL
140+
)
99141
next(csv_reader)
100142
for row in csv_reader:
101-
row['Network Name'] = net['name']
102-
row['Network ID'] = net['id']
143+
row["Network Name"] = net["name"]
144+
row["Network ID"] = net["id"]
103145
csv_writer.writerow(row)
104146

105147

106-
if __name__ == '__main__':
148+
if __name__ == "__main__":
107149
start_time = datetime.now()
108150
main()
109151
end_time = datetime.now()

0 commit comments

Comments
 (0)