08-01-2023 10:13 AM
Necesito hacer un respaldo automatico en un swtich que se guarde en un servidor TFTP y se conecte via ssh, ya que no pude con el comando KRON porque aparecen vacios los archivos que guarda. Intente hacer un script que es el siguiente (por motivos de seguridad quite el usuario y la contraseña"
#-*- coding: utf-8 -*-
import paramiko
import datetime
import os
# Set up SSH connection parameters
user = 'usuario'
secret = 'contraseña'
port = 22
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
# Define variables
time_now = datetime.datetime.now().strftime('%m_%d_%Y_%H_%M_%S')
infilepath = "C:\\Program Files\\Tftpd64\\"
outfilepath = "C:\\Program Files\\Tftpd64\\"
devicelist = "device-list1.txt"
tftp_server_ip = "10.18.245.6"
tftp_server_directory = "C:\\Program Files\\Tftpd64\\"
# Open device file
with open(infilepath + devicelist, "r") as input_file:
iplist = input_file.readlines()
# Loop through device list and execute commands
for ip in iplist:
ipaddr = ip.strip()
ssh.connect(hostname=ipaddr, username=user, password=secret, port=port)
stdin, stdout, stderr = ssh.exec_command('show run')
backup_file_name = ipaddr + "_" + time_now + ".txt"
with open(outfilepath + backup_file_name, "w") as outfile:
for line in stdout:
outfile.write(line)
ssh.close()
# Upload backup file to TFTP server
transport = paramiko.Transport((tftp_server_ip, 69)) # Puerto 69 es el estándar para TFTP
transport.connect(username='', password='')
sftp = transport.open_sftp()
sftp.put(outfilepath + backup_file_name, tftp_server_directory + backup_file_name)
sftp.close()
transport.close()
Pero ocurre este error
Traceback (most recent call last):
actively refused it
08-01-2023 10:27 AM
Hi @l19550794
The error "Unable to connect to 10.18.245.6:" seems that the script could not connect to the TFTP server.
If you run the command on the switch "copy running-config tftp:" are you able to save the config on the TFTP ?
08-01-2023 10:29 AM
Si, si se guarda
08-01-2023 10:38 AM - edited 08-01-2023 10:46 AM
I think you are closing the SSH session in the wrong place.
Loop through device list and execute commands
for ip in iplist:
ipaddr = ip.strip()
ssh.connect(hostname=ipaddr, username=user, password=secret, port=port)
stdin, stdout, stderr = ssh.exec_command('show run')
backup_file_name = ipaddr + "_" + time_now + ".txt"
with open(outfilepath + backup_file_name, "w") as outfile:
for line in stdout:
outfile.write(line)
ssh.close() -
If you close the SSH here, theorically you are disconnecting from the switch and then the script is trying to access the TFTP from your machine.
I believe you should close the SSH session after the TFTP session
# Upload backup file to TFTP server
transport = paramiko.Transport((tftp_server_ip, 69)) # Puerto 69 es el estándar para TFTP
transport.connect(username='', password='')
sftp = transport.open_sftp() ==Here you are using SFTP which does not use Port 69 but port 22
sftp.put(outfilepath + backup_file_name, tftp_server_directory + backup_file_name)
sftp.close()
transport.close()
08-01-2023 10:45 AM
Ya lo intente y no.
08-01-2023 11:01 AM
Can you share the script after the changes?
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide