I have a script that uses SSH via Paramiko to login to switches and initiate a TFTP copy of the startup-config. This almost works like I want except the file name does not include the .txt file type.
The specific line I'm working with is:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i))
Tried looking at examples on Google.
#!/usr/bin/env python3
###############################################
# THIS SCRIPT SAVES THE RUNNING-CONFIG TO #
# STARTUP-CONFIG AND THEN COPIES THE #
# STARTUP-CONFIG TO THE SPECIFIED IP VIA TFTP #
###############################################
#
import paramiko
import sys, time
#
username = 'user'
password = 'pass'
#
with open('Advantech_Test_IPs', 'r') as host_list:
for i in host_list:
str(i)
try:
ssh_client = paramiko.SSHClient()
ssh_client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh_client.connect(i, username=username, password=password)
command = ssh_client.invoke_shell()
command.send("copy running-config startup-config \n")
time.sleep(1)
command.send("copy startup-config tftp://.../Backup-{}.txt \n".format(i))
time.sleep(1)
ssh_client.close
print(i, "BACKUP COMPLETE")
except paramiko.ssh_exception.NoValidConnectionsError as e:
pass
print(i, "No Valid Connections")
except paramiko.ssh_exception.AuthenticationException as ea:
print(i, "Authentication Failed")
except paramiko.ssh_exception.BadHostKeyException as eb:
print(i, "Bad Host Key")
except Exception as ex:
print(i, "SOMETHING WENT WRONG!")
print("All done.")
The file gets copied over, but the .txt file extension is not appended so I end up with file types that match the last part of the IP address.
The i
contains a new line at the end. So effectively you are sending a two lines to the server:
copy startup-config tftp://192.168.0.1/Backup-IP
.txt
You can use str.strip
(or str.rstrip
) to remove the newline:
command.send("copy startup-config tftp://192.168.0.1/Backup-{}.txt \n".format(i.strip()))