-
Notifications
You must be signed in to change notification settings - Fork 0
/
NCM_Config_Backup.py
88 lines (75 loc) · 3.51 KB
/
NCM_Config_Backup.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import requests
import json
import os
from datetime import datetime
server = 'https://www.cradlepointecm.com/api/v2' # US0
# server = 'https://www.us2.cradlepointecm.com/api/v2' # US2
api_keys = {'X-ECM-API-ID': 'YOUR',
'X-ECM-API-KEY': 'KEYS',
'X-CP-API-ID': 'GO',
'X-CP-API-KEY': 'HERE',
'Content-Type': 'application/json'}
# Create Backup Directories
backups_dir = os.getcwd() + '/NCM Config Backups'
if not os.path.exists(backups_dir):
os.makedirs(backups_dir)
timestamp = datetime.now().strftime("%m-%d-%Y %I.%M.%S%p").lower()
my_backup_dir = f'{backups_dir}/{timestamp}'
os.makedirs(my_backup_dir) # Timestamped Backup Directory
routers_dir = f'{my_backup_dir}/routers'
groups_dir = f'{my_backup_dir}/groups'
os.makedirs(routers_dir) # Subdirectory for routers
os.makedirs(groups_dir) # Subdirectory for groups
print('\n¸,ø¤°º¤ø,¸¸,ø¤º°`° NCM Config Backup °º¤ø,¸¸,ø¤º°`°º¤ø,¸\n')
print(f'Creating Backups Here: \n{my_backup_dir}/\n')
print('Backing up device configurations...\n')
routers_backed_up = 0
routers_url = f'{server}/routers/?limit=500'
while routers_url:
get_routers = requests.get(routers_url, headers=api_keys)
if get_routers.status_code < 300:
get_routers = get_routers.json()
routers = get_routers["data"]
routers = [x for x in routers if x["state"] != "initialized"]
for router in routers:
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()
try:
config = get_config["data"][0]["configuration"]
if config != [{}, []]:
with open(f'{routers_dir}/{router["id"]} - {router["name"]}.json', 'wt') as f:
f.write(json.dumps(config))
print(f'Backed up config for router : {router["id"]} - {router["name"]}')
routers_backed_up += 1
except Exception as e:
print(f'Exception backing up config for {router["id"]} - {router["name"]}: {e}')
else:
print(f'Error getting config for {router["id"]} - {router["name"]}: {get_config.text}')
routers_url = get_routers["meta"]["next"]
else:
print(f'Error getting routers: {get_routers.text}')
print(f'\nBacked up {routers_backed_up} router configurations.')
print('\nBacking up group configurations...\n')
groups_backed_up = 0
groups_url = f'{server}/groups/?limit=500'
while groups_url:
get_groups = requests.get(groups_url, headers=api_keys)
if get_groups.status_code < 300:
get_groups = get_groups.json()
groups = get_groups["data"]
for group in groups:
config = group["configuration"]
if config != [{}, []]:
group_name = group["name"].replace('/', '_')
with open(f'{groups_dir}/{group["id"]} - {group_name}.json', 'wt') as f:
f.write(json.dumps(config))
print(f'Backed up config for group : {group["id"]} - {group["name"]}')
groups_backed_up += 1
groups_url = get_groups["meta"]["next"]
else:
print(f'Error getting groups: {get_groups.text}')
print(f'\nBacked up {routers_backed_up} router configurations.')
print(f'\nBacked up {groups_backed_up} group configurations.')
print('\nNCM Config Backup Complete!')