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