Python與Cisco的事兒之四

來源:互聯網
上載者:User

標籤:err   bin   tftp服務   ram   set   txt   1.5   use   bsp   

   以下代碼實現的流程: cdp -->擷取相應連結的資訊-->自動寫進裝置相對應的連接埠--->configure儲存-->configure備份到TFTP伺服器!

#!/usr/bin/pythonfrom netmiko import ConnectHandlerfrom netmiko.ssh_exception import NetMikoTimeoutExceptionimport timeimport sysimport getpassimport datetimeclass CiscoNetwork():        def __init__(self,username,password):                 self.username = username                 self.password = password        def CiscoDevice(self,iplist):                 self.device={'device_type':'cisco_ios',                              'username':self.username,                              'password':self.password,                              'ip':iplist                             }                 print('-'*100)                 print "[+]connect to network device... %s" %(iplist)                 self.connect = ConnectHandler(**self.device)                 self.connect.enable()        def gethostname(self):                self.hostname = self.connect.find_prompt()                self.hostname = self.hostname.replace("#","")                print self.hostname        def cdpconfig(self):                self.output = self.connect.send_command('show cdp nei')                lines = self.output.splitlines()[5:-2]                #print lines                hostname = None                config = []                for line in lines:                    words = line.split()                    if len(words) == 1:                        hostname = words[0].split('.')[0]                    elif hostname is None:                        hostname = words[0].split('.')[0]                        local = ''.join(words[1:3])                        remote = ''.join(words[-2:])                        description = '_'.join((hostname,remote))                        config.append('interface' + local)                        config.append('description' + description)                        hostname = None                    else:                         local = ''.join(words[0:2])                         remote = ''.join(words[-2:])                         description = '_'.join((hostname,remote))                         config.append('interface' + local)                         config.append('description' + description)                         hostname = None                self.config ='\n'.join(config)                def configure(self):                configure = self.connect.send_config_set(self.config)                saveconfigure = self.connect.send_command('write  memory')                def backup(self,tftpip):                 date = datetime.datetime.now().strftime('%Y-%m-%d')                 bkhostname   = '_'.join((self.hostname,date))                 backupcmd    =  'copy running-config tftp:'                 self.tftpip  =  tftpip                 bkconf  = '\n'.join((backupcmd,self.tftpip,bkhostname))                 self.bkconf  = self.connect.send_command(bkconf)        def close(self):                if self.connect is not None:                       self.connect.disconnect()                       self.connect = None                       print '[+] Network devices complete all settings!'if __name__ == '__main__':        print "[+] This Program is beging done......."        #username = raw_input('Username:')        #password = getpass.getpass()        for iplist in open("/opt/other/ip.txt"):                try:                   switch = CiscoNetwork('admin','Password.123')                   switch.CiscoDevice(iplist)                   switch.gethostname()                   switch.cdpconfig()                   switch.configure()                   switch.backup('172.16.200.201')                   switch.close()                except (EOFError, NetMikoTimeoutException):                   print ('Can not connect to Device!')

     

Python與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.