-
Notifications
You must be signed in to change notification settings - Fork 0
/
postCloneSwitch.py
134 lines (110 loc) · 5 KB
/
postCloneSwitch.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# title: Cisco Meraki Switch Device Cloning
# description: This script will clone a source switch to one or more target switches within the same organization.
import requests
from tqdm import tqdm
import include.config as config
# Default API key (you can replace this with your default key)
DEFAULT_API_KEY = config.DEFAULT_API_KEY
BASE_URL = config.BASE_URL
# Function to fetch organizations
def get_organizations(api_key):
headers = {'X-Cisco-Meraki-API-Key': api_key}
response = requests.get(f'{BASE_URL}/organizations', headers=headers)
response.raise_for_status()
return response.json()
# Function to fetch MS devices (switches) within an organization
def get_ms_devices(api_key, org_id):
headers = {'X-Cisco-Meraki-API-Key': api_key}
response = requests.get(f'{BASE_URL}/organizations/{org_id}/devices', headers=headers)
response.raise_for_status()
return [device for device in response.json() if device['model'][:2] == 'MS']
# Function to clone a switch to target devices
def clone_switch(api_key, org_id, source_serial, target_serials):
headers = {'X-Cisco-Meraki-API-Key': api_key}
source_device = next((device for device in get_ms_devices(api_key, org_id) if device['serial'] == source_serial), None)
if source_device:
print(f"Cloning source switch: {source_device['name']} ({source_device['serial']})")
for target_serial in tqdm(target_serials, desc="Cloning Progress"):
if target_serial != source_serial:
payload = {
'name': source_device['name'],
'cloneFromSerial': source_device['serial'],
'copyPortConfigs': True,
'copySwitchSettings': True,
}
response = requests.post(f'{BASE_URL}/devices/{target_serial}/clone', headers=headers, json=payload)
response.raise_for_status()
else:
print(f"Source switch with serial {source_serial} not found.")
# Function to list registered switches
def list_registered_switches(ms_devices):
print('List of registered MS devices (switches):')
for i, device in enumerate(ms_devices, start=1):
print(f'{i}. {device["name"]} ({device["serial"]})')
# Main function
def main():
# Banner message
print('Cisco Meraki Switch Device Cloning Script')
print('Developer: Mohd NeoTech <mohdneotech@gmail.com>')
print('-----------------------------------------------')
# Prompt for API key choice
use_default_api_key = input('Do you want to use the default API key? (y/n): ').strip().lower()
if use_default_api_key == 'y':
api_key = DEFAULT_API_KEY
else:
api_key = input('Enter your Meraki API key: ')
# Fetch organizations
try:
organizations = get_organizations(api_key)
if not organizations:
print('No organizations found. Please check your API key or organization permissions.')
return
except requests.exceptions.RequestException as e:
print(f'Error fetching organizations: {e}')
return
# Prompt for organization selection
print('Select an organization:')
for i, org in enumerate(organizations, start=1):
print(f'{i}. {org["name"]} ({org["id"]})')
org_choice = int(input('Enter the number of the organization: '))
if org_choice < 1 or org_choice > len(organizations):
print('Invalid organization choice.')
return
selected_org = organizations[org_choice - 1]
org_id = selected_org['id']
# Fetch and list MS devices within the selected organization
try:
ms_devices = get_ms_devices(api_key, org_id)
if not ms_devices:
print('No MS devices (switches) found in the organization.')
return
except requests.exceptions.RequestException as e:
print(f'Error fetching MS devices: {e}')
return
list_registered_switches(ms_devices)
# Prompt for source switch selection
source_choice = int(input('Enter the number of the source switch to clone: '))
if source_choice < 1 or source_choice > len(ms_devices):
print('Invalid source switch choice.')
return
source_serial = ms_devices[source_choice - 1]['serial']
# Prompt for target switches selection (multiple)
print('Select one or more target switches to clone to (enter 0 when done):')
target_serials = []
while True:
target_choice = int(input('Enter the number of a target switch or 0 to finish: '))
if target_choice == 0:
break
if target_choice < 1 or target_choice > len(ms_devices):
print('Invalid target switch choice.')
else:
target_serial = ms_devices[target_choice - 1]['serial']
target_serials.append(target_serial)
if not target_serials:
print('No valid target switches selected.')
return
# Clone switches with progress bar
clone_switch(api_key, org_id, source_serial, target_serials)
print('Switch cloning completed.')
if __name__ == '__main__':
main()