继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

Python 备份cisco ios 设备 简例

慕桂英4014372
关注TA
已关注
手记 65
粉丝 9
获赞 55

netmik库用来ssh,os库用来管理文件夹,datetime用于生成文件名
.

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()    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)        breakfor 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 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 !

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP