Two things about Python and Cisco

Source: Internet
Author: User

Stick to the continuous Python war cisconetwork.

#!/usr/bin/python from netmiko import connecthandlerfrom netmiko.ssh_exception  Import netmikotimeoutexceptionimport timeimport sysimport getpassimport reimport  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 ()         &nbsP;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)              &nbsP;    soft  = re.findall (' l3_.+ ', 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                         if  __name__ ==  ' __main__ ':p rint  "[+] this program is beging done ...]         username = raw_input (' username: ')          password = getpass.getpass () For iplist in open ("/opt/ Other/ip.txt "):             try:                  switch =  Cisconetwork (Username,password)  switch. Ciscodevice (IPList)                   switch.gethostname ()  switch.interfaceinfo (' Show ip int&nbSp;brief ')                   switch.show (' show version ')                   switch.close ()               except  (eoferror, netmikotimeoutexception):                  print  (' Can not connect to  device ')

Two things 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.