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