python netmiko實現cisco網路裝置配置備份

來源:互聯網
上載者:User

標籤:code   open   path   oauth   find   col   lock   message   tin   

import timefrom netmiko import ConnectHandlerfrom netmiko.ssh_exception import NetMikoTimeoutExceptionfrom netmiko.ssh_exception import NetMikoAuthenticationExceptionimport sysimport getpassfrom datetime import datetimeimport osdef NetworkDevice(username,password,iplist,enablepass): rt = { ‘device_type‘:‘cisco_ios‘, ‘username‘:username, ‘password‘:password, ‘ip‘: iplist, ‘secret‘:enablepass } print(‘-‘ * 50) print(u‘[+] connecting to network device {0}...‘.format(iplist)) net_connect = ConnectHandler(**rt) net_connect.enable() hostname = net_connect.find_prompt().replace("#", "") print (u‘[+] hostname:{0}‘.format(hostname)) timestr = time.strftime(‘%Y-%m-%d-%H-%M-%S‘,time.localtime(time.time())) cmd = ‘show running‘ filename = (u‘{0}_{1}_{2}.txt‘.format(iplist,cmd,timestr)) filepath = r‘C:\netpy\5pyresult\/‘ if os.path.exists(filepath): message = ‘OK,the "%s" dir exists.‘ else: message = "Now, I will create the %s" os.makedirs(filepath) save = open(filepath + filename,‘w‘) print(u‘[+] executing {0} command‘.format(cmd)) output = net_connect.send_command(cmd) time.sleep(2) save.write(output) print(u‘[+] {0} command executed,result was saved at {1}!‘.format(cmd,filename)) save.close() net_connect.disconnect()if __name__ == ‘__main__‘: print "[+] The Program is running......." username = raw_input(‘Username:‘) password = getpass.getpass() enablepass = raw_input(‘enable:‘) filepath2 = r‘C:\netpy\\‘ if os.path.exists(filepath2): message = ‘OK,the "%s" file exists.‘ else: message = "Now, I will create the %s" os.makedirs(filepath2) for ips in open(r‘C:\netpy\iplist.txt‘,‘r‘): start_time = datetime.now() iplist = ips.replace(‘\n‘, ‘‘) try: NetworkDevice(username,password,iplist,enablepass) except (EOFError, NetMikoTimeoutException): print (‘Can not connect to Device‘) except (EOFError, NetMikoAuthenticationException): print (‘username/password wrong!‘) except (ValueError,NetMikoAuthenticationException): print (‘enable password wrong!‘) print "Time elapsed: {0}\n".format(datetime.now() - start_time) time.sleep(2)


79837161

python netmiko實現cisco網路裝置配置備份

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.