The third thing about Python and Cisco

Source: Internet
Author: User

The following code can be implemented after logging on to the network device through the show CDP nei command to view the neighbor device, and then use stitching to add the description, and then write into the corresponding network device interface.

#!/usr/bin/pythonfrom netmiko import connecthandlerfrom netmiko.ssh_exception import  netmikotimeoutexceptionimport timeimport sysimport getpassimport stringioimport  pprint class 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)               &nbSp;   self.connect = connecthandler (**self.device)                   self.connect.enable ()          def gethostname (self):                 self.hostname = self.connect.find_prompt ()                 self.hostname  = self.hostname.replace ("#", "")                          def show (self):                 self.output =  Self.connect.send_command (' Show cdp nei ')                 &nbSp;lines = self.output.splitlines () [5:-2]                 hostnames = None                 config = []                 for line in lines:                      words = line.split ()                      if len (words)  == 1:                          hostnames = words[0].split ('. ') [0]                    elif hostnames is none:                          Hostnames = words[0].split ('. ') [0]                         local =  '. Join (Words[1:3])                           remote =  ". Join (words[-2:])                          description =  ' _ '. Join ((hostnames,remote))                          config.append (' interface '  +  '   '  + local)                          config.append (' description '  + '   '  +  Description)                          hostnames = None                     else:                           local =  '. Join (Words[0:2])                           remote  =  ". Join (words[-2:])                           description =  ' _ '. Join ((Hostnames,remote))                           config.append (' interface '  +  '   ' + local)                            config.append (' description '  +  '   '  + description)                           hostnames = None                 self.config= ' \ n '. Join (config)                  def configure (self):       &nBsp;         configure = self.connect.send_config_set ( Self.config)                  Saveconfigure = self.connect.send_command (' write  memory ')                  print  configure                 print saveconfigure                        def close (self):                 if self.connect is not None:                         Self.connect.disconnect () &NBsp;                       self.connect = Noneif __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.show ()                     switch.configure ()                     switch.close ()                  except  ( Eoferror, netmikotimeoutexception):                    print  (' Can not connect to device ')





The third thing about Python and Cisco

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.