Fin du TP2
This commit is contained in:
Binary file not shown.
@@ -48,6 +48,23 @@ def create_vlan_config_cpe_paris():
|
||||
R3_config = render_network_config(template_name='vlan_router.j2', data=R3_data)
|
||||
return R3_config,esw3_config
|
||||
|
||||
def create_ospf_config_cpe_marseille():
|
||||
R1_data = load_json_data_from_file(file_path='data/ospf_R01.json')
|
||||
R1_config = render_network_config(template_name='config_ospf.j2', data=R1_data)
|
||||
|
||||
return R1_config
|
||||
|
||||
def create_ospf_config_cpe_paris():
|
||||
R2_data = load_json_data_from_file(file_path='data/ospf_R02.json')
|
||||
R2_config = render_network_config(template_name='config_ospf.j2', data=R2_data)
|
||||
|
||||
return R2_config
|
||||
|
||||
def create_ospf_config_cpe_lyon():
|
||||
R3_data = load_json_data_from_file(file_path='data/ospf_R03.json')
|
||||
R3_config = render_network_config(template_name='config_ospf.j2', data=R3_data)
|
||||
|
||||
return R3_config
|
||||
|
||||
if __name__ == "__main__":
|
||||
"""
|
||||
@@ -60,3 +77,12 @@ if __name__ == "__main__":
|
||||
r03_config, esw3_config = create_vlan_config_cpe_paris()
|
||||
save_built_config('config/vlan_R03.conf', r03_config)
|
||||
save_built_config('config/vlan_ESW3.conf', esw3_config)
|
||||
|
||||
R1_ospf_config = create_ospf_config_cpe_marseille()
|
||||
save_built_config('config/ospf_R1.conf', R1_ospf_config)
|
||||
R2_ospf_config = create_ospf_config_cpe_paris()
|
||||
save_built_config('config/ospf_R2.conf', R2_ospf_config)
|
||||
R3_ospf_config = create_ospf_config_cpe_lyon()
|
||||
save_built_config('config/ospf_R3.conf', R3_ospf_config)
|
||||
|
||||
|
||||
|
||||
@@ -1,56 +1,123 @@
|
||||
import json
|
||||
from napalm import get_network_driver
|
||||
|
||||
def save_built_config(file_name, data):
|
||||
with open(file_name, "w") as f:
|
||||
f.write(data)
|
||||
return file_name
|
||||
|
||||
def get_inventory():
|
||||
pass
|
||||
|
||||
"""
|
||||
Lit le fichier inventory/hosts.json et retourne son contenu.
|
||||
"""
|
||||
inventory_file = "inventory/hosts.json"
|
||||
try:
|
||||
with open(inventory_file, "r") as f:
|
||||
data = json.load(f)
|
||||
return data
|
||||
except FileNotFoundError:
|
||||
print(f"Erreur : le fichier {inventory_file} n'existe pas.")
|
||||
return []
|
||||
|
||||
def get_json_data_from_file(file):
|
||||
pass
|
||||
|
||||
def question_26(device):
|
||||
pass
|
||||
|
||||
command = ['show ip interface brief']
|
||||
output = device.cli(command)
|
||||
|
||||
def question_27(device):
|
||||
pass
|
||||
|
||||
|
||||
def question_28(device):
|
||||
pass
|
||||
command = ['show ip interface brief']
|
||||
output = device.cli(command)
|
||||
print(type(output))
|
||||
|
||||
def question_29(device):
|
||||
pass
|
||||
#output = device.get_config()
|
||||
output = device.get_arp_table()
|
||||
print(output)
|
||||
|
||||
|
||||
def question_30(device):
|
||||
pass
|
||||
output = device.get_arp_table()
|
||||
print(type(output))
|
||||
|
||||
|
||||
def question_31():
|
||||
pass
|
||||
device.load_merge_candidate(filename='config/loopback_R01.conf')
|
||||
print(device.compare_config())
|
||||
device.commit_config()
|
||||
|
||||
|
||||
def question_32():
|
||||
pass
|
||||
def question_33():
|
||||
r01 = {
|
||||
'hostname': '172.16.100.62',
|
||||
'username': 'cisco',
|
||||
'password': 'cisco'
|
||||
}
|
||||
r02 = {
|
||||
'hostname': '172.16.100.190',
|
||||
'username': 'cisco',
|
||||
'password': 'cisco'
|
||||
}
|
||||
r03 = {
|
||||
'hostname': '172.16.100.254',
|
||||
'username': 'cisco',
|
||||
'password': 'cisco'
|
||||
}
|
||||
|
||||
routers = {'1': r01, '2': r02, '3': r03}
|
||||
|
||||
def question_34():
|
||||
pass
|
||||
for i in range(1, 4):
|
||||
driver = get_network_driver('ios')
|
||||
device = driver(**routers[str(i)])
|
||||
device.open()
|
||||
device.load_merge_candidate(filename=f'config/ospf_R{i}.conf')
|
||||
print(device.compare_config())
|
||||
device.commit_config()
|
||||
device.close()
|
||||
|
||||
def question_35():
|
||||
liste_hosts = get_inventory()
|
||||
|
||||
for host in liste_hosts:
|
||||
nom_host = host['hostname']
|
||||
|
||||
connexion_info = {
|
||||
'hostname': host['ip'],
|
||||
'username': host['username'],
|
||||
'password': host['password']
|
||||
}
|
||||
|
||||
try:
|
||||
driver_ios = get_network_driver('ios')
|
||||
device = driver_ios(**connexion_info)
|
||||
device.open()
|
||||
config_sauvegarde = device.get_config()
|
||||
|
||||
with open(f'backup/{nom_host}.bak', 'w') as fichier_sauvegarde:
|
||||
fichier_sauvegarde.write(str(config_sauvegarde))
|
||||
|
||||
device.close()
|
||||
|
||||
except Exception as erreur:
|
||||
print(f"Erreur lors de la connexion à {nom_host} : {erreur}")
|
||||
continue
|
||||
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
r01 = {
|
||||
'hostname':'xx.xx.xx.xx',
|
||||
'username': "xx",
|
||||
'password': "xx"
|
||||
'hostname':'172.16.100.62',
|
||||
'username': "cisco",
|
||||
'password': "cisco"
|
||||
}
|
||||
|
||||
# driver = get_network_driver('ios')
|
||||
# device = driver(**r01)
|
||||
# device.open()
|
||||
driver = get_network_driver('ios')
|
||||
device = driver(**r01)
|
||||
device.open()
|
||||
|
||||
#question_26(device)
|
||||
#question_27(device)
|
||||
@@ -58,5 +125,5 @@ if __name__ == "__main__":
|
||||
#question_29(device)
|
||||
#question_30(device)
|
||||
#question_31()
|
||||
#question_32()
|
||||
#question_34()
|
||||
#question_33()
|
||||
question_35()
|
||||
@@ -139,6 +139,42 @@ def question_21():
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Erreur sur {device['hostname']} : {e}")
|
||||
def question_22():
|
||||
inventory = get_inventory()
|
||||
|
||||
for device in inventory:
|
||||
# On ne traite pas les routeurs dont le hostname != 'R1' de ESW1
|
||||
if 'R1' == device["hostname"] or 'ESW1' == device["hostname"]:
|
||||
continue
|
||||
|
||||
print(f"\n=== Connexion au routeur {device['hostname']} ({device['ip']}) ===")
|
||||
|
||||
try:
|
||||
# Paramètres de connexion
|
||||
device_params = {
|
||||
'device_type': device['device_type'],
|
||||
'host': device['ip'],
|
||||
'username': device['username'],
|
||||
'password': device['password']
|
||||
}
|
||||
net_connect = ConnectHandler(**device_params)
|
||||
net_connect.enable()
|
||||
|
||||
# affiche le hostname
|
||||
print(f"Connexion réussie sur {device['hostname']}")
|
||||
|
||||
config_file = f'config/vlan_{device["hostname"]}.conf'
|
||||
output = net_connect.send_config_from_file(config_file)
|
||||
print(f"\nRésultat de l'application de la configuration sur {device['hostname']} :\n{output}")
|
||||
|
||||
# Sauvegarde la configuration
|
||||
save_output = net_connect.save_config()
|
||||
print(f"\nSauvegarde de la configuration sur {device['hostname']} :\n{save_output}")
|
||||
|
||||
net_connect.disconnect()
|
||||
|
||||
except Exception as e:
|
||||
print(f"❌ Erreur sur {device['hostname']} : {e}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
|
||||
@@ -154,7 +190,7 @@ if __name__ == "__main__":
|
||||
#question_17(net_connect)
|
||||
#question_18(net_connect)
|
||||
hosts = get_inventory()
|
||||
print(hosts)
|
||||
#print(hosts)
|
||||
#question_20()
|
||||
#question_21()
|
||||
net_connect.disconnect()
|
||||
question_22()
|
||||
Reference in New Issue
Block a user