-
Notifications
You must be signed in to change notification settings - Fork 0
/
cisco_management.py
278 lines (232 loc) · 9.07 KB
/
cisco_management.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#Version 1.1
#25/03/2020
#License GPL.3-0
#Author romeo-network
#Importation des modules nécessaires.
from netmiko import ConnectHandler
from netmiko.ssh_exception import NetMikoTimeoutException
from getpass import getpass
from datetime import datetime
import getpass
import calendar
import time
import os
import errno
import sys
#Récupération des informations d'identifications
print("Entrez vos informations de connexion: ")
username = input("Username: \n")
password = getpass.getpass("Password: \n")
#Fonction destinée à propulser un fichier de configuration vers les équipements.
def configuration():
start_time = datetime.now()
#Ouverture du fichier de configuration et du fichier contenant les IP des commutateurs.
with open('CONFIGURATION_L2') as f:
lines_l2 = f.read().splitlines()
with open('switch.list') as f:
ip_sw = f.read().splitlines()
#Boucle autant de fois qu'il y a de ligne dans le fichier 'switch.list'.
for ip in ip_sw:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
#Vérifie la présence d'une ligne de configuration issu du fichier 'CONFIGURATION_L2', si résultat positif: n'envoie rien.
#Sauvegarde la 'running-config' dans la 'startup-config' dans les 2 cas.
try:
result= net_connect.send_command("show running-config | inc ip access-list standard SSH")
except (NetMikoTimeoutException):
print ("Délai d'attente dépassé. " +ip)
if "ip access-list standard SSH" in result:
net_connect.save_config()
time.sleep(0.5)
net_connect.disconnect()
#Envoie l'intégralité du contenu du fichier de configuration.
else:
output = net_connect.send_config_set(lines_l2)
time.sleep(1)
net_connect.save_config()
time.sleep(0.5)
net_connect.disconnect()
print(output)
#Ouverture du fichier de configuration et du fichier contenant les IP des routeurs.
with open('CONFIGURATION_L3') as f:
lines = f.read().splitlines()
with open('router.list') as f:
ip_rtr = f.read().splitlines()
#Boucle autant de fois qu'il y a de ligne dans 'router.list'.
for ip in ip_rtr:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
#Vérifie la présence d'une ligne de configuration issue de 'CONFIGURATION_L3', si résultat positif; n'envoie rien.
#Sauvegarde la 'running-config' dans la 'startup-config' dans les 2 cas.
try:
result= net_connect.send_command("show running-config | inc ip access-list standard SSH")
except (NetMikoTimeoutException):
print ("Délai d'attente dépassé. " + ip)
if "ip access-list standard SSH" in result:
net_connect.save_config()
time.sleep(0.5)
net_connect.disconnect()
#Envoie l'intégralité du contenu du fichier de configuration
else:
output = net_connect.send_config_set(lines)
time.sleep(1)
net_connect.save_config()
time.sleep(0.5)
net_connect.disconnect()
print(output)
end_time = datetime.now()
total_time = end_time - start_time
print("Durée de la configuration: "+str(total_time)+"\n")
#Fonction destinée à redémarrer les équipements.
def reload():
with open('router.list') as f:
ip_rtr = f.read().splitlines()
with open('switch.list') as f:
ip_sw = f.read().splitlines()
ip_add = ip_rtr + ip_sw
#Pour chaque ligne présente, boucle et envoie des instructions de redémarrage.
for ip in ip_add:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
net_connect.send_command('reload in 5', '\n')
time.sleep(0.5)
net_connect.send_command('no\n', expect_string='confirm')
time.sleep(0.5)
net_connect.send_command('\n')
time.sleep(0.5)
net_connect.disconnect()
#Fonction destiné à supprimer les fichiers de configurations des équipements.
#Appelle la fonction 'reload()' une fois terminée.
def wipeout():
start_time = datetime.now()
with open('switch.list') as f:
ip_sw = f.read().splitlines()
#Boucle autant de fois qu'il y a de ligne, et envoie des instructions d'effacement des fichiers vlan.dat et startup-config.
for ip in ip_sw:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
net_connect.send_command('delete vlan.dat', '\n')
time.sleep(0.5)
net_connect.send_command('vlan.dat\n', expect_string='confirm')
time.sleep(0.5)
net_connect.send_command('\n')
time.sleep(0.5)
net_connect.send_command('write erase', expect_string='confirm')
time.sleep(0.5)
net_connect.send_command('\n')
net_connect.disconnect()
with open('router.list') as f:
ip_rtr = f.read().splitlines()
#Boucle autant de fois qu'il y a de ligne, et envoie des instructions d'effacement de la startup-config.
for ip in ip_rtr:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
net_connect.send_command('write erase', expect_string='confirm')
time.sleep(0.5)
net_connect.send_command('\n')
net_connect.disconnect()
#Appelle la fonction 'reload()'
reload()
end_time = datetime.now()
total_time = end_time - start_time
print("Durée de l'effacement: "+str(total_time)+"\n")
#Fonction destinée à sauvegarder les configurations des équipements
def save():
start_time = datetime.now()
with open('router.list') as f:
ip_rtr = f.read().splitlines()
with open('switch.list') as f:
ip_sw = f.read().splitlines()
#Addition de liste.
ip_add = ip_rtr + ip_sw
#Boucle autant de fois qu'il y a de ligne dans la liste.
#Récupère les configurations des équipements.
for ip in ip_add:
device = {
'device_type': 'cisco_ios',
'ip': ip,
'username': username,
'password': password,
}
net_connect = ConnectHandler(**device)
#Récupération du hostname pour nommer le dossier de sauvegarde
def hostname():
sh_hstn = net_connect.send_command("show running-config | in hostname")
hostname = sh_hstn.split()
return hostname[1]
#Ici, on obtient l'équivalent d'un 'mkdir -p' pour l'idempotence.
try:
os.makedirs("/home/cisco/" + hostname())
except OSError as exc:
if exc.errno == errno.EEXIST and os.path.isdir("/home/cisco/" + hostname()):
pass
#Création des fichiers en local, et y incorpore le résultat du 'show running-config'.
bckp = net_connect.send_command("show running-config")
now = datetime.now()
date = now.strftime("%d_%m_%Y_%H:%M:%S")
bckp_path = "/home/cisco/{0}/{1}.txt".format(hostname(),date)
with open(bckp_path, "a") as file:
file.write(bckp + "\n")
net_connect.disconnect()
end_time = datetime.now()
total_time = end_time - start_time
print("Durée de la sauvegarde: "+str(total_time)+"\n")
#Fonction servant à obtenir un menu, en bouclant à l'infini sur '0'.
def main():
choice ='0'
while choice =='0':
print("Menu principal: choix disponible 1 à 4. \n")
print("Choisir '1' pour la configuration avancée. ")
print("Choisir '2' pour la sauvegarde intégrale. ")
print("Choisir '3' pour l'effacement intégral. ")
print("Choisir '4' pour quitter le script. \n")
choice = input ("Faites un choix: ")
if choice == "4":
print("\nFin du script.")
sys.exit()
elif choice == "3":
print("\nEffacement intégral. ")
wipeout()
time.sleep(0.5)
main()
elif choice == "2":
print("\nSauvegarde intégrale. ")
save()
time.sleep(0.5)
main()
elif choice == "1":
print("\nConfiguration des IOS. ")
configuration()
time.sleep(0.5)
main()
else:
print("\nChoix invalide. \n")
main()
main()