The Netmik library is used for Ssh,os libraries to manage folders, and datetime is used to generate file names
.
from netmiko import ConnectHandlerimport osimport datetime#define device typecisco = { ‘device_type‘:‘cisco_ios‘, ‘ip‘:‘‘, ‘username‘:‘admin‘, ‘password‘:‘ccie‘, ‘secret‘:‘ccie‘,}
.
#get IP address from a fileip_addr = open ("g:\\pythonproject_netmiko\\iplist.txt") path = "G:\\pythonproject_netmiko\\" Folder_v = Os.listdir () #查看當前文件夾所有文件while true:print ("Input your Bak file folder name:") folder_name = input () i F folder_name in Folder_v:print ("There are a same name file already, please input again!") Else:create_path = path + folder_name os.mkdir (create_path) print ("Created folder:" +folder_name+ "su ccessfully!\n ") Print (" Note:the bakup file'll be is in: ", Create_path) breakfor IP in ip_addr:cisco[' IP '] = Ip.strip (' \ n ') print (' Connecting: ' +ip.strip ("\ n") + "...") Net_connect = Connecthandler (**cisco) print ("Connect Ed successfully! ") Net_connect.enable () Run_config = Net_connect.send_command (' show run ') Save_name = Ip.strip () + datetime.datetime.no W (). Strftime ("-%y%m%d-%h_%m_%s") print (' Backing Up: ' +ip.strip ("\ n") + ' ... ') f = open (Create_path + "\ \" +save_name+ ". T XT ", ' W ') F.write (Run_conFIG) F.close () print ("Backed up successfully!") f = open (Create_path + "\\0_IP-date-time.fileType", ' W ') #used to Instructf.close () Ip_addr.close ()
.
For minding codes
print("\n\n\nBacked up all devices compeleted ! ")print("Please check the bakeup file in the follow folder: "+create_path)
.
over~ good luck to you !
Python backup Cisco IOS device brief example