cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
619
Views
0
Helpful
5
Replies

Script para respaldo automatico en switch via ssh a un servidor tftp

l19550794
Level 1
Level 1

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):

File "C:\Program Files\Tftpf64\Script.py" line 41, in <module>
"C:\Users\Administrator\AppData\Local\Programs\Python\Python311\Lib\site-packages\paramiko\transport.py
 line 448, in __init__
raise SSHexception (
paramiko.ssh_exception.SSHException: Unable to connect to 10.18.245.6:
[WinError 10061] No connection could be made because the target machine

actively refused it

5 Replies 5

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 ?

 
 

l19550794
Level 1
Level 1

Si, si se guarda

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()

l19550794
Level 1
Level 1

Ya lo intente y no.

Can you share the script after the changes?