The five things that Python and Cisco

Source: Internet
Author: User

The following code tests the method of a single subnet loop:

#!/usr/bin/python from netmiko import connecthandlerfrom netmiko.ssh_exception  Import netmikotimeoutexceptionfrom ipy import ipimport timeimport sysimport  getpassimport reimport io import stringioclass 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  Interfaceinfo (Self,cmd):                  result = self.connect.send_command (CMD)                   for interface in result.split (' \ n ') :                      if  ' Up '  in interface:                            #print  interface                            lines=stringio.stringio (interface)                             data = lines.read ()                            intername =   '   ' Join (Re.findall (' ^eth.+\/\d ', data))                            loopback  =  '   '. Join ( Re.findall (' loopback[0-9] ', data))                            interIP =  Re.findall (  ' \. Join ([' \d{1,3} ']*4), data)                            if intername:                               PRINT INTERNAME&NBSP, ': ',  '. Join (InterIP)                             else:                              print  loopback  ,  ': ',  '. Join (Interip)          def  show (Self,cmd):                  result = self.connect.send_command (CMD)                   lines = stringio.stringio (Result)                   data =  lines.read ()                   uptime = re.findall (' uptime.+ ', data)                   id     = re.findall (' \d{8} ', data)                  soft  =  Re.findall (' L3_.+\\.bin ', data)                   print  ' device uptime: ',  '. Join (UPtime)                   print  ' device id: ',  '. Join (ID)                   print  ' soft version: ', '. Join (Soft)         def close ( Self):                if  self.connect is not none:                        self.connect.disconnect ()                          self.connect  = None                        print  ' [+] network devices complete  All settings! '                       if __name__ ==  ' __main__ ':p rint  "[+] this program is  beging done.          #username  = raw_input ( ' Username: ')          #password  = getpass.getpass ()          ipadd = ip (' 10.0.0.0/30 ')          for ips in ipadd:              Ipadds = stringio.stringio (IPs)              ipadds =  ' \ n '. Join (Ipadds). Splitlines ()     for iplist in  Ipadds:                try:                      switch = cisconetwork (' admin ', ' password.123 ')                      switch. Ciscodevice (IPList)                      switch.gethostname ()                      switch.interfaceinfo (' show ip int  Brief ')       &nbsP;             switch.show (' show  Version ')                      switch.close ()                  except  (eoferror, netmikotimeoutexception):                     print (' Can not  connect to device! ')

How to increase the number of subnets requires more than one layer of loops, the code changes as follows:

if __name__ ==  ' __main__ ':p rint  "[+] this program is beging done ... "         #username  = raw_input (' username: ')           #password  = getpass.getpass ()          ipadd = ipset ([IP (' 10.0.0.0/30 '), IP (' 10.0.1.0/30 '), IP (' 10.0.2.0/30 ')])          for ips in ipadd:             ipadd = stringio.stringio (IPs)              ipadds =  ' \ n '. Join (Ipadd). Splitlines ()              for ip01 in ipadds:                 ip02=ip (IP01)                  for ip03 in ip02:                     ip03 =  Stringio.stringio (IP03)                      ip03 =  ' \ n '. Join (IP03). Splitlines ()              for iplist in ip03:                          try:                             switch = cisconetwork ( ' admin ', ' password.123 ')                               switch. Ciscodevice (IPList)                              switch.gethostname ()                               switch.interfaceinfo (' Show ip int brief ')                               switch.show (' show version ')                               switch.close ()                          except  (eoferror, netmikotimeoutexception):                              print (' can not connect to device! ')


The five things that 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.