Created
May 18, 2024 02:18
-
-
Save Franklin13620/dc50dbed311d90394496348ae9a0b02a to your computer and use it in GitHub Desktop.
Mass download of backups for Cisco equipment with Netmiko / Descarga masiva de copias de seguridad para equipos Cisco con Netmiko
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import datetime | |
import os | |
from netmiko import ConnectHandler, redispatch | |
# Extraer fecha y hora actual | |
tiempo_actual = datetime.datetime.now() | |
formateo_date_time = tiempo_actual.strftime("%B-%d-%Y - %I.%M.%Ss %p") | |
# Ruta | |
ruta_backup = "C:\\Users\\Users\\Desktop\\Prueba\\" | |
# Lista de loopbacks # | |
loopback_list = [ | |
'IP-1', | |
'IP-2' | |
] | |
# Inicio de procesos | |
for loopback in loopback_list: | |
try: | |
# Conectarse a routers | |
net_connect = ConnectHandler( | |
device_type='cisco_ios', | |
ip=loopback, | |
username='USER', | |
password='PASSWORD', | |
global_delay_factor=2 | |
) | |
print("Conectandose al router.... " + loopback) | |
# Se envian los comandos | |
net_connect.send_config_set("terminal length 0", read_timeout=4) | |
sh_run = net_connect.send_command("sh run") | |
sh_hostname = net_connect.send_command("sh run | i hostname") | |
# Replazar hostname | |
hostname = sh_hostname.replace("hostname", "") | |
# Crear carpetas | |
nombre_carpeta = os.path.join(ruta_backup, loopback, hostname) | |
try: | |
os.makedirs(nombre_carpeta, exist_ok=True) # Crea carptea sino existe | |
except OSError as e: | |
print(f"Error al crear carpeta {nombre_carpeta}: {e}") | |
# Ruta de la carpeta | |
ruta_carpeta = os.path.join(nombre_carpeta, loopback + " " + formateo_date_time + ".txt") | |
# Guardar el backup dentro de la carpeta del hostname del equipo cisco | |
with open(ruta_carpeta, 'w') as archivo: | |
archivo.write(sh_run) | |
print(f"Se guardo copia de seguridad correctamente: {ruta_carpeta}") | |
except Exception as e: | |
print(f"Error al conectar o guardar copia de seguridad: {loopback}: {e}") | |
finally: | |
# Cerrar sesion | |
net_connect.disconnect() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment