def sw_delete_unneeded(self): """ Delete unneeded firmware We keep the one pointed to by "boot system flash" and the currently running one status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug( "------------------- sw_delete_unneeded() -------------------") keep = {} bootflash = {} deleted = [] files = self.swList() # Get the boot flash image lines = self.getRunningConfig(filter_="^boot system flash") for line in lines: filename = line[18:].strip() if filename in files: # file found in flash, candidate to keep if len(keep) == 0: keep[filename] = True bootflash[filename] = line # Check the currently running firmware lines = self.getCommandOutput("show version", filter_="^System image file is") if len(lines) < 1: raise self.ElementException( "Unexpected state, can't find command that selects operating system (1)" ) line = lines[0].strip() p = line.find(":") if p < 0: raise self.ElementException( "Unexpected state, can't find command that selects operating system (2)" ) filename = line[p + 1:-1] if filename[0] == "/": filename = filename[1:] if filename in files: keep[filename] = "" else: log.warning( "Host %s running firmware (%s) does not exist in flash" % (self.hostname, filename)) for f in files: if f in keep: log.debug("Keeping file %s" % f) else: log.debug("Deleting file %s" % f) deleted.append(f) self.swDelete(f) if f in bootflash: self.setConfig("no " + bootflash[f]) return deleted
def sw_delete_unneeded(self): """ Delete unneeded firmware We keep the one pointed to by "boot system flash" and the currently running one status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_delete_unneeded() -------------------") keep = {} bootflash = {} deleted = [] files = self.swList() # Get the boot flash image lines = self.getRunningConfig(filter_="^boot system flash") for line in lines: filename = line[18:].strip() if filename in files: # file found in flash, candidate to keep if len(keep) == 0: keep[filename] = True bootflash[filename] = line # Check the currently running firmware lines = self.getCommandOutput("show version", filter_="^System image file is") if len(lines) < 1: raise self.ElementException("Unexpected state, can't find command that selects operating system (1)") line = lines[0].strip() p = line.find(":") if p < 0: raise self.ElementException("Unexpected state, can't find command that selects operating system (2)") filename = line[p+1:-1] if filename[0] == "/": filename = filename[1:] # print("line", line) # print("filename", filename) if filename in files: keep[filename] = "" else: log.warning("Host %s running firmware (%s) does not exist in flash" % (self.hostname, filename)) for f in files: if f in keep: log.debug("Keeping file %s" % f) else: log.debug("Deleting file %s" % f) deleted.append(f) self.swDelete(f) if f in bootflash: self.setConfig("no " + bootflash[f]) return deleted
def l2_peers(self, interface=None, default_domain=None): """ Returns the device L2 neighbours, LLDP and CDP """ peers = emtypes.Peers() # ----- LLDP ----- if interface: cmd = "show lldp neighbors %s detail" % interface else: cmd = "show lldp neighbors detail" lines = self.run(cmd=cmd) sections = self._l2_peers_get_sections(lines) ix = 0 for start,stop in sections: peer = emtypes.Peer(local_if="") for line in lines[start:stop]: # print(line) if line.startswith("Local Intf:"): peer.local_if = line[11:].strip() elif line.startswith("Chassis id:"): peer.remote_mac = line[11:].strip() elif line.startswith("Port id:"): peer.remote_if = line[8:].strip() elif line.startswith("System Name:"): peer.remote_hostname = line[12:].strip() if default_domain and "." not in peer.remote_hostname: peer.remote_hostname += "." + default_domain elif line.startswith("System Description:"): peer.remote_description = lines[start+1] elif line.startswith("Management Addresses:"): peer.remote_ipaddr = lines[start + 1].strip().split()[1] start += 1 if peer.local_if: peers.add(peer) else: log.warning("Cannot add peer, no local_if. %s" % peer) # ----- CDP ----- if interface: cmd = "show cdp neighbors %s detail" % interface else: cmd = "show cdp neighbors detail" lines = self.run(cmd=cmd) sections = self._l2_peers_get_sections(lines) ix = 0 for start,stop in sections: peer = emtypes.Peer(remote_mac="") for line in lines[start:stop]: # print(line) if line.startswith("Device ID:"): peer.remote_hostname = line[11:].strip() if default_domain and "." not in peer.remote_hostname: peer.remote_hostname += "." + default_domain if line.startswith("Platform:"): peer.remote_description = line[9:].strip().split(",")[0] elif line.startswith("Interface:"): tmp = line.split() peer.local_if = tmp[1].replace(",", "") peer.remote_if = tmp[6].replace(",", "") elif line.startswith(" IP address:"): peer.remote_ipaddr = line[13:].strip() if peer.local_if: if not peers.exists(peer): peers.add(peer) else: log.warning("Peer %s already added, through LLDP" % peer) else: log.warning("Cannot add peer, no local_if. %s" % peer) return peers