


Lines 20–22 open the device file using the variable name and read the contents into a list called iplist that we will use in the next section of code. Be sure to provide the full path to the file. This is required to escape the backslash character. Notice the double backslashes in the file path. I have variables to represent the file paths to our input text file as well as the location where we will store the backup files of the Cisco config. I try to use variable names that make sense and make it easy to figure out what they represent. Using variables makes our program easier to read and gives it more flexibility. Lines 14–17 define some variables we will use in our script. We use the datetime function to give us a timestamp for our filename. Lines 4–11 import Paramiko and the time module and set up the SSH parameters for Paramiko. Here is the breakdown of each line in the script: Outfile = open(outfilepath + ipaddr + "_" + time_now, "w") Stdin, stdout, stderr = ssh.exec_command('show run') Ssh.connect(hostname=ipaddr, username=user, password=secret, port=port)

# loop through device list and execute commands Input_file = open( infilepath + devicelist, "r") Outfilepath = "c:\\Users\\johnk\\Downloads\\" Infilepath = "c:\\Users\\johnk\\Downloads\\" Ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy()) # import modules needed and set up ssh connection parameters Cisco config backup Python script # Backup Cisco config I have an explanation of each line after the code. Python ignores white space, but it makes it easier to read and understand. I have added comments and broken them into sections, separating the lines with a line of white space. Much of this was used in the previous script. Now the file starts coming down with no need to do anything more.Now, let us have a look at the script we will use. Now the command with the actual information in.Ĭopy scp://JDOE: 173.255.173.150/ 8a.bin flash0:// 8a.bin To download the file to the Cisco device I use the command: All I have to do is connect up (username/password) to my server IP address. This allows me to upload the file to my server very easily. To upload files to the server, I use WINSCP ( ). An easier solution is to have any standard SSH server (Linux, Unix) and copy the files to and from the server. Doing so, I found TFTP or evening having a TFTP server problematic. Typically I am having to upload files a Cisco device across the Internet.
