Python 备份cisco ios 设
短信预约 -IT技能 免费直播动态提醒
netmik库用来ssh,os库用来管理文件夹,datetime用于生成文件名
.
from netmiko import ConnectHandler
import os
import datetime
#define device type
cisco = {
'device_type':'cisco_ios',
'ip':'',
'username':'admin',
'password':'ccie',
'secret':'ccie',
}
.
#get ip address from a file
ip_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()
if folder_name in folder_v:
print("there is a same name file already, please input again!")
else:
create_path = path + folder_name
os.mkdir(create_path)
print("Created folder:"+folder_name+" successfully!\n")
print("NOTE:the bakup file will be in: ",create_path)
break
for ip in ip_addr:
cisco['ip'] = ip.strip('\n')
print('Connecting: '+ip.strip("\n")+"...")
net_connect = ConnectHandler(**cisco)
print("Connected successfully !")
net_connect.enable()
run_config = net_connect.send_command('show run')
save_name = ip.strip() + datetime.datetime.now().strftime("-%Y%m%d-%H_%M_%S")
print('Backing up: '+ip.strip("\n")+'...')
f = open(create_path +"\\"+save_name+".txt",'w')
f.write(run_config)
f.close()
print("Backed up successfully!")
f = open(create_path +"\\0_IP-date-time.fileType",'w')#used to instruct
f.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 !
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341