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, let us have a look at the script we will use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |