Python and Cisco's Thing Four

Source: Internet
Author: User

The following code implements the process: CDP--and get the corresponding link information--automatic write-in device corresponding to the port--->configure save-->configure Backup to the TFTP server!

#!/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 ')                &nbsP; lines = self.output.splitlines () [5:-2]                  #print  lines                 hostname = None                 config = []                 for line in lines:                      words = line.split ()                      if len (words)  == 1:                          hostname =&Nbsp;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: '    &NBsp;             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 ')            &nbSp;        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 and Cisco's Thing Four

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.