-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathname_devices.py
70 lines (56 loc) · 2.26 KB
/
name_devices.py
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
#!/usr/bin/env python3
import traceback
import json
from unicodedata import normalize
import dotenv
import meraki
from pyzabbix import ZabbixAPI, ZabbixAPIException
from tqdm import tqdm
config = dotenv.dotenv_values()
client = meraki.DashboardAPI(
config['MERAKI_API_KEY'],
output_log=False,
nginx_429_retry_wait_time=3,
action_batch_retry_wait_time=3,
print_console=False,
suppress_logging=True,
maximum_retries=10)
# zabbix = ZabbixAPI(config['ZABBIX_URL'])
# zabbix.login(api_token=config['ZABBIX_API_TOKEN'])
# dashboard = meraki.DashboardAPI(config['MERAKI_API_KEY'])
networks = client.organizations.getOrganizationNetworks(
config['ORG_ID'], total_pages='all'
)
# with open('networks-3.txt', 'w', encoding='UTF-8') as f:
# f.writelines(json.dumps(networks))
def main() -> None:
# renamed_count = 0
# max_renames = 5
output = []
for network in networks:
network_id = network['id']
network_name = network['name']
# Get the devices in the network
devices = client.networks.getNetworkDevices(network_id)
for device in devices:
# Check if the device is an MX model and has no name (name is null or empty)
if 'MX' in device['model'] and not device.get('name'):
# Generate the new name
new_name = f"{network_name} - {device['model']}"
# Update the device name
client.devices.updateDevice(device['serial'], name=new_name)
print(f"Renamed {device['serial']} to {new_name}")
# renamed_count += 1
output.append(new_name)
# Check if we've renamed 5 devices
# if renamed_count >= max_renames:
# print(f"Renamed {max_renames} devices. Stopping script.")
# break
# Stop processing further networks if the limit is reached
# if renamed_count >= max_renames:
# break
with open('new_name-1.json', 'w', encoding='UTF-8') as f:
f.writelines(json.dumps(output))
print("Device renaming complete.")
if __name__ == "__main__":
main()