|
14 | 14 | def main(): |
15 | 15 | # Instantiate a Meraki dashboard API session |
16 | 16 | 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/", |
19 | 19 | output_log=True, |
20 | 20 | log_file_prefix=os.path.basename(__file__)[:-3], |
21 | | - log_path='', |
22 | | - print_console=False |
| 21 | + log_path="", |
| 22 | + print_console=False, |
23 | 23 | ) |
24 | 24 |
|
25 | 25 | # Get list of organizations to which API key has access |
26 | 26 | organizations = dashboard.organizations.getOrganizations() |
27 | 27 |
|
28 | 28 | # Iterate through list of orgs |
29 | 29 | 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"] |
32 | 32 |
|
33 | 33 | # Get list of networks in organization |
34 | 34 | try: |
35 | 35 | networks = dashboard.organizations.getOrganizationNetworks(org_id) |
36 | 36 | 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}") |
41 | 41 | continue |
42 | 42 | except Exception as e: |
43 | | - print(f'some other error: {e}') |
| 43 | + print(f"some other error: {e}") |
44 | 44 | continue |
45 | 45 |
|
46 | 46 | # 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}" |
49 | 49 | if folder_name not in os.listdir(): |
50 | 50 | os.mkdir(folder_name) |
51 | 51 |
|
52 | 52 | # Iterate through networks |
53 | 53 | total = len(networks) |
54 | 54 | counter = 1 |
55 | | - print(f' - iterating through {total} networks in organization {org_id}') |
| 55 | + print(f" - iterating through {total} networks in organization {org_id}") |
56 | 56 | 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})") |
58 | 58 | try: |
59 | 59 | # 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 | + ) |
62 | 66 | 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}") |
67 | 71 | except Exception as e: |
68 | | - print(f'some other error: {e}') |
| 72 | + print(f"some other error: {e}") |
69 | 73 | else: |
70 | 74 | if clients: |
71 | 75 | # 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 | + ) |
74 | 80 | 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 | + ) |
77 | 89 | csv_writer.writeheader() |
78 | 90 | csv_writer.writerows(clients) |
79 | 91 | output_file.close() |
80 | | - print(f' - found {len(clients)}') |
| 92 | + print(f" - found {len(clients)}") |
81 | 93 |
|
82 | 94 | counter += 1 |
83 | 95 |
|
84 | 96 | # 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 | + ] |
89 | 122 | field_names.insert(0, "Network Name") |
90 | 123 | field_names.insert(1, "Network ID") |
91 | 124 |
|
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 | + ) |
93 | 133 | csv_writer.writeheader() |
94 | 134 | for net in networks: |
95 | | - file_name = f'{net["name"]}.csv' |
| 135 | + file_name = f"{net['name']}.csv" |
96 | 136 | 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 | + ) |
99 | 141 | next(csv_reader) |
100 | 142 | 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"] |
103 | 145 | csv_writer.writerow(row) |
104 | 146 |
|
105 | 147 |
|
106 | | -if __name__ == '__main__': |
| 148 | +if __name__ == "__main__": |
107 | 149 | start_time = datetime.now() |
108 | 150 | main() |
109 | 151 | end_time = datetime.now() |
|
0 commit comments