|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
| 3 | +import meraki |
| 4 | +from pathlib import Path |
| 5 | +import json |
| 6 | + |
3 | 7 | read_me = '''
|
4 |
| -A Python 3 script to pull of the enabled BSSID from a specified network. |
| 8 | +A Python 3 script to pull of the enabled BSSID from an Organization. |
5 | 9 |
|
6 | 10 | Required Python modules:
|
7 | 11 | meraki
|
8 | 12 |
|
9 | 13 | Usage:
|
10 |
| -bssid.py Network Name |
| 14 | +bssid.py |
11 | 15 |
|
12 |
| -If you have only one Organization, it will search for the Network name. |
| 16 | +If you have only one Organization, it will find all BSSID in each Network |
| 17 | +and create a csv for each in Documents/BSSID/Organization |
13 | 18 |
|
14 | 19 | If you have multiple Organizations, it will ask you which org to run against
|
15 | 20 |
|
|
18 | 23 |
|
19 | 24 | '''
|
20 | 25 |
|
21 |
| -import meraki |
22 |
| -import sys |
23 |
| -from pathlib import Path |
24 |
| -import json |
25 |
| - |
26 |
| -net_list = {} |
27 | 26 | p = Path.home()
|
28 | 27 | loc = p / 'Documents' / 'BSSID'
|
29 | 28 |
|
30 | 29 | dashboard = meraki.DashboardAPI(suppress_logging=True)
|
31 | 30 |
|
32 | 31 |
|
33 |
| -def getLocation1(): |
| 32 | +def base_folder(): |
34 | 33 | if not Path.is_dir(loc):
|
35 | 34 | Path.mkdir(loc)
|
36 | 35 |
|
37 | 36 |
|
38 |
| -def getlocation2(loc2): |
| 37 | +def get_orgs(): |
| 38 | + orgs = dashboard.organizations.getOrganizations() |
| 39 | + org_dict = {} |
| 40 | + for i in orgs: |
| 41 | + org_dict[i['id']] = i['name'] |
| 42 | + return org_dict |
| 43 | + |
| 44 | + |
| 45 | +def find_org(org_dict): |
| 46 | + if len(org_dict) == 1: |
| 47 | + org_id = org_dict[0]['id'] |
| 48 | + org_name = org_dict[0]['name'] |
| 49 | + else: |
| 50 | + org_id = input( |
| 51 | + f"Please type the number of the Organization you want to find " |
| 52 | + f"the bssid in{json.dumps(org_dict, indent=4)}" "\n") |
| 53 | + org_name = org_dict.get(org_id) |
| 54 | + return org_id, org_name |
| 55 | + |
| 56 | + |
| 57 | +def org_folder(org_name): |
| 58 | + loc2 = Path.joinpath(loc, org_name) |
39 | 59 | if not Path.is_dir(loc2):
|
40 | 60 | Path.mkdir(loc2)
|
41 | 61 |
|
42 | 62 |
|
43 |
| -def getOrgs(): |
44 |
| - orgs = dashboard.organizations.getOrganizations() |
45 |
| - if len(orgs) == 1: |
46 |
| - for dic in orgs: |
47 |
| - orgID = dic['id'] |
48 |
| - orgName = dic['name'] |
49 |
| - loc2 = loc / orgName |
50 |
| - getlocation2(loc2) |
51 |
| - getNetworks(orgID, orgName) |
52 |
| - else: |
53 |
| - org_list = {} |
54 |
| - for dic in orgs: |
55 |
| - org_list[dic['id']] = dic['name'] |
56 |
| - orgID = input(f'Please type in the number of the Organization name ' |
57 |
| - f'that you would like to query {json.dumps(org_list, indent=4)}' "\n") |
58 |
| - orgName = org_list.get(orgID) |
59 |
| - loc2 = loc / orgName |
60 |
| - getlocation2(loc2) |
61 |
| - getNetworks(orgID, orgName) |
62 |
| - |
63 |
| - |
64 |
| -def getNetworks(orgID, orgName): |
65 |
| - networks = dashboard.organizations.getOrganizationNetworks( |
66 |
| - orgID, total_pages='all') |
67 |
| - for dic in networks: |
68 |
| - if 'wireless' in dic['productTypes']: |
69 |
| - net_list[dic['id']] = dic['name'] |
70 |
| - for k, v in net_list.items(): |
71 |
| - net_id = k |
72 |
| - net_name = v |
73 |
| - getAP(net_id, net_name, orgName) |
74 |
| - |
75 |
| - |
76 |
| -def getAP(net_id, net_name, orgName): |
77 |
| - devices = dashboard.networks.getNetworkDevices(net_id) |
78 |
| - ap_list = {} |
79 |
| - for dic in devices: |
80 |
| - model = dic['model'][:2] |
81 |
| - if model == 'MR' or model == 'CW': |
82 |
| - if dic.get('name') is None: |
83 |
| - ap_list[dic['mac']] = dic['serial'] |
84 |
| - else: |
85 |
| - ap_list[dic['name']] = dic['serial'] |
86 |
| - getBss(net_name, orgName, ap_list) |
87 |
| - |
88 |
| - |
89 |
| -def getBss(net_name, orgName, ap_list): |
90 |
| - bss = f'{loc}/{orgName}/{net_name}.csv' |
91 |
| - with open(bss, mode='w') as f: |
92 |
| - f.write(f"AP Name , SSID Name , Frequency , BSSID" + "\n") |
93 |
| - for k, v in ap_list.items(): |
94 |
| - response = dashboard.wireless.getDeviceWirelessStatus(v) |
95 |
| - for data in response['basicServiceSets']: |
96 |
| - good = data['enabled'] |
| 63 | +def get_networks(org_id): |
| 64 | + net_list = dashboard.organizations.getOrganizationNetworks( |
| 65 | + org_id, total_pages='all') |
| 66 | + return net_list |
| 67 | + |
| 68 | + |
| 69 | +def find_networks(net_list): |
| 70 | + net_ids = {} |
| 71 | + for i in net_list: |
| 72 | + if 'wireless' in i['productTypes']: |
| 73 | + net_ids[i['id']] = i['name'] |
| 74 | + net_name = i['name'] |
| 75 | + return net_ids, net_name |
| 76 | + |
| 77 | + |
| 78 | +def find_ap(net_ids): |
| 79 | + ap_dict = {} |
| 80 | + for k, v in net_ids.items(): |
| 81 | + name = v |
| 82 | + lst = [] |
| 83 | + devices = dashboard.networks.getNetworkDevices(k) |
| 84 | + for i in devices: |
| 85 | + model = i['model'][:2] |
| 86 | + if model == 'MR' or model == 'CW': |
| 87 | + dic = {} |
| 88 | + if i.get('name') is None: |
| 89 | + dic.update(name=i['mac'], serial=i['serial']) |
| 90 | + else: |
| 91 | + dic.update(name=i['name'], serial=i['serial']) |
| 92 | + lst.append(dic) |
| 93 | + ap_dict[name] = lst |
| 94 | + return ap_dict |
| 95 | + |
| 96 | + |
| 97 | +def get_bssid(ap_dict, net_ids): |
| 98 | + bssid_dict = {} |
| 99 | + for k, v in net_ids.items(): |
| 100 | + name = v |
| 101 | + lst = [] |
| 102 | + for data in ap_dict[name]: |
| 103 | + ap = data['serial'] |
| 104 | + response = dashboard.wireless.getDeviceWirelessStatus(ap) |
| 105 | + for value in response['basicServiceSets']: |
| 106 | + info_dict = {} |
| 107 | + good = value['enabled'] |
97 | 108 | if good is True:
|
98 |
| - f.write(f"{k} , {data['ssidName']} , {data['band']}\ |
99 |
| - , {data['bssid']}" + "\n") |
100 |
| - print(f'Your file {net_name}.csv has been creeated in {loc} / {orgName}') |
| 109 | + info_dict['name'] = data['name'] |
| 110 | + info_dict['ssidName'] = value['ssidName'] |
| 111 | + info_dict['band'] = value['band'] |
| 112 | + info_dict['bssid'] = value['bssid'] |
| 113 | + lst.append(info_dict) |
| 114 | + bssid_dict[name] = lst |
| 115 | + return bssid_dict |
| 116 | + |
| 117 | + |
| 118 | +def file_writer(bssid_dict, net_ids, org_name): |
| 119 | + for k, v in net_ids.items(): |
| 120 | + network = v |
| 121 | + file = f'{loc}/{org_name}/{network}.csv' |
| 122 | + with open(file, mode='w') as f: |
| 123 | + f.write(f"AP Name , SSID Name , Frequency , BSSID" + "\n") |
| 124 | + for data in bssid_dict[network]: |
| 125 | + f.write(f"{data['name']}, {data['ssidName']}, " |
| 126 | + f"{data['band']}, {data['bssid']}" + "\n") |
| 127 | + print(f'Your file {network}.csv has been created in {loc} / {org_name}') |
| 128 | + |
| 129 | + |
| 130 | +def main(): |
| 131 | + base_folder() |
| 132 | + org_dict = get_orgs() |
| 133 | + org_id, org_name = find_org(org_dict) |
| 134 | + org_folder(org_name) |
| 135 | + net_list = get_networks(org_id) |
| 136 | + net_ids, net_name = find_networks(net_list) |
| 137 | + ap_dict = find_ap(net_ids) |
| 138 | + bssid_dict = get_bssid(ap_dict, net_ids) |
| 139 | + print(f'writing BSSID to file') |
| 140 | + file_writer(bssid_dict, net_ids, org_name) |
101 | 141 |
|
102 | 142 |
|
103 | 143 | if __name__ == '__main__':
|
104 |
| - getLocation1() |
105 |
| - getOrgs() |
| 144 | + main() |
0 commit comments