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