-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbulk_config.py
More file actions
88 lines (80 loc) · 2.75 KB
/
bulk_config.py
File metadata and controls
88 lines (80 loc) · 2.75 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# bulk_config.py - bulk configure devices in NCM from .csv file
#
# 1. Create routers.csv with router IDs listed in column A and other
# device-specific values in subsequent columns (B, C, D, etc)
# 2. Use NCM Config Editor to build a config template, then click
# "View Pending Changes" and copy the config
# 3. Paste your config below in the build_config() function and replace
# config values with corresponding csv column letters
# 4. Enter API Keys and run script
#
# Example config for a csv file with hostname in column B:
#
# [{
# "system": {
# "system_id": column["B"]
# }
# },
# []
# ]
import requests
import csv
csv_file = 'routers.csv'
api_keys = {
'X-ECM-API-ID': 'YOUR',
'X-ECM-API-KEY': 'KEYS',
'X-CP-API-ID': 'GO',
'X-CP-API-KEY': 'HERE'
}
def build_config(column):
# Paste your configuration BELOW "return \"
return \
[{
"system": {
"system_id": column["B"]
}
},
[]
]
# Paste configuration ABOVE HERE ^
# Replace config values with corresponding csv column letters
def load_csv(filename):
router_configs = {}
try:
with open(filename, 'rt') as f:
rows = csv.reader(f)
for row in rows:
column = {"A": row[0]}
i = 1
while True:
try:
column[chr(i + 97).upper()] = row[i]
column[chr(i + 97).lower()] = row[i]
i += 1
except:
break
router_configs[column["A"]] = column
except Exception as e:
print(f'Exception reading csv file: {e}')
return router_configs
server = 'https://www.cradlepointecm.com/api/v2'
rows = load_csv(csv_file)
for router_id in rows:
config_url = f'{server}/configuration_managers/?router={router_id}'
get_config = requests.get(config_url, headers=api_keys)
if get_config.status_code < 300:
get_config = get_config.json()
config_data = get_config["data"]
config_id = config_data[0]["id"]
config = build_config(rows[router_id])
patch_config = requests.patch(f'{server}/configuration_managers/'
f'{config_id}/', headers=api_keys,
json={"configuration": config})
if patch_config.status_code < 300:
print(f'Sucessfully patched config to router: {router_id}')
else:
print(f'Error patching config {router_id}: {patch_config.text}')
else:
print(f'Error getting configuration_managers/ ID for {router_id}: '
f'{get_config.text}')
print('Done!')