Python 備份cisco ios 裝置 簡例

來源:互聯網
上載者:User

標籤:plist   com   ssh   str   write   project   strftime   filetype   save   

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 !

Python 備份cisco ios 裝置 簡例

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.