def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it """ self.connect() log.debug("------------------- reload() -------------------") self.em.writeln("reload") while True: match = self.em.expect( { "confirm": "yes/no", "reloading": "confirm", "reloading1": "closed by remote host", "reloading2": "closed by foreign host", "reloading3": "Reload command.", }) log.debug("Reload match: %s" % match) if match == "reloading": self.em.writeln("y") elif match == "confirm": # configuration is not saved, do so if save_config: self.em.writeln("y") else: self.em.writeln("n") else: break # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def sw_list(self, filter_=None, callback=None): """ Get a list of all firmware in the element status: OK """ self.connect() log.debug("------------------- sw_list() -------------------") self.em.writeln("dir") self.em.expect("name") self.em.expect("#") msg = self.em.before # self.wait_for_prompt() # lets parse names, we ignore a bunch of names and directories sw_list = [] if filter_: r = re.compile(filter_) for line in msg.split("\r\n"): line = line.rstrip() if line: tmp = line.split() if len(tmp) != 6: continue if tmp[1] == "<DIR>": continue f = tmp[5] if filter_: if r.search(f): sw_list.append(f) else: sw_list.append(f) return sw_list
def sw_set_boot(self, filename, callback=None): """ Check if filename exist in element if true configure element to boot with the filename status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_set_boot() -------------------") if not self.sw_exist(filename): raise self.ElementException( "Error cant change boot software, filename %s does not exist" % filename) # remove old boot system flash commands # todo # startup system-software S5300EI-V200R003C00SPC300.cc lines = self.get_running_config(filter_="^boot system flash ") for line in lines[1:]: print(" no " + line) self.setConfig("no " + line) # set new boot system flash cmd = "boot system flash %s" % filename self.setConfig(cmd) self.wait_for_prompt()
def sw_list(self, filter_=None, callback=None): """ Get a list of all firmware in the element status: OK """ self.connect() log.debug("------------------- sw_list() -------------------") self.em.writeln("dir") self.em.expect("name") self.em.expect("#") msg = self.em.before # self.wait_for_prompt() # lets parse names, we ignore a bunch of names and directories sw_list = [] if filter_: r = re.compile(filter_) for line in msg.split("\r\n"): line = line.rstrip() if line: tmp = line.split() if len(tmp) != 6: continue if tmp[1] == "<DIR>": continue f = tmp[5] if filter_: if r.search(f): sw_list.append(f) else: sw_list.append(f) return sw_list
def configure(self, config_lines=None, save_running_config=False, callback=None): """ Reconfigure device todo: trigger on '%-ERR: <error description>' """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure terminal") match = self.em.expect("\(config\)#") if match is None: raise self.ElementException( "Error Could not enter configuration mode") for config_line in config_lines: self.em.writeln(config_line) match = self.em.expect("\)#") if match is None: raise self.ElementException( "Error waiting for next configuration prompt") self.em.writeln("end") self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def sw_list(self, filter_=None, callback=None): """ Get a list of all firmware in the element status: OK """ self.connect() log.debug("------------------- sw_list() -------------------") self.em.writeln("dir flash:") self.em.expect("FileName") self.em.expect(" total ") msg = self.em.before self.wait_for_prompt() # lets parse names, we ignore a bunch of names and directories sw_list = [] if filter_: r = re.compile(filter_) for line in msg.split("\r\n"): line = line.rstrip() if line: tmp = line.split() if tmp[1:3] == ['KB', 'total']: break if tmp[1][0] == "d": # directory? continue f = tmp[7] if filter_: if r.search(f): sw_list.append(f) else: sw_list.append(f) return sw_list
def sw_delete(self, filename): """ Delete filename from flash status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_delete() -------------------") if not self.sw_exist(filename): raise self.ElementException("File %s not found in flash" % filename) # todo, check so we dont remove the current filename # conf = self.getRunningConfig(filter="^boot system flash") cmd = "delete flash:%s" % filename self.em.writeln(cmd) match = self.em.expect({ "confirm": r"Delete filename.*\?" }) if match is None: raise self.ElementException("Error deleting filename %s" % filename) if match == "confirm": self.em.writeln("") match = self.em.expect({ "confirm": "Delete.*\[confirm\]", }) if match is None: raise self.ElementException("Unexpected response, seach buffer: %s" % self.em.before) self.em.write("y") # confirm deletion self.wait_for_prompt()
def sw_list(self, filter_=None, callback=None): """ Get a list of all firmware in the element status: OK """ self.connect() log.debug("------------------- sw_list() -------------------") self.em.writeln("dir flash:") self.em.expect("FileName") self.em.expect(" total ") msg = self.em.before self.wait_for_prompt() # lets parse names, we ignore a bunch of names and directories sw_list = [] if filter_: r = re.compile(filter_) for line in msg.split("\r\n"): line = line.rstrip() if line: tmp = line.split() if tmp[1:3] == ['KB', 'total']: break if tmp[1][0] == "d": # directory? continue f = tmp[7] if filter_: if r.search(f): sw_list.append(f) else: sw_list.append(f) return sw_list
def sw_delete(self, filename): """ Delete filename from flash status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_delete() -------------------") if not self.sw_exist(filename): raise self.ElementException("File %s not found in flash" % filename) # todo, check so we dont remove the current filename # conf = self.getRunningConfig(filter="^boot system flash") cmd = "delete flash:%s" % filename self.em.writeln(cmd) match = self.em.expect({ "confirm": r"Delete filename.*\?" }) if match is None: raise self.ElementException("Error deleting filename %s" % filename) if match == "confirm": self.em.writeln("") match = self.em.expect({ "confirm": "Delete.*\[confirm\]", }) if match is None: raise self.ElementException("Unexpected response, seach buffer: %s" % self.em.before) self.em.write("y") # confirm deletion self.wait_for_prompt()
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it """ self.connect() log.debug("------------------- reload() -------------------") self.em.writeln("reload") while True: match = self.em.expect( { "confirm": "yes/no", "reloading": "confirm", "reloading1": "closed by remote host", "reloading2": "closed by foreign host", "reloading3": "Reload command.", }) log.debug("Reload match: %s" % match) if match == "reloading": self.em.writeln("y") elif match == "confirm": # configuration is not saved, do so if save_config: self.em.writeln("y") else: self.em.writeln("n") else: break # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def wait_for_prompt(self): log.debug( "------------------- wait_for_prompt(%s) -------------------" % self.hostname) if not self._wait_for_prompt: raise self.ElementException("Not implemented") match = self.em.expect(self._wait_for_prompt) return match
def sw_copy_from(self, mgr=None, filename=None, callback=None): """ Copy file from element """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_copy_from() -------------------") if not self.sw_exist(filename): raise self.ElementException("File %s does not exist on element" % filename)
def sw_delete_unneeded(self, callback=None): """ Delete unneeded firmware We keep the one pointed to by "boot system flash" and the currently running one """ self.connect() keep = {} bootflash = {} deleted = [] files = self.sw_list() # 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.sw_delete(f) if f in bootflash: self.configure("no " + bootflash[f]) return deleted
def sw_copy_from(self, mgr=None, filename=None, callback=None): """ Copy file from element """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_copy_from() -------------------") if not self.sw_exist(filename): raise self.ElementException("File %s does not exist on element" % filename)
def disconnect(self): """ Disconnect from the element """ log.debug("------------------- disconnect(%s) -------------------" % self.hostname) if self.transport: # self.em.writeln("logout") self.em = None self.transport.disconnect() self.transport = None
def disconnect(self): """ Disconnect from the element """ log.debug("------------------- disconnect(%s) -------------------" % self.hostname) if self.transport: # self.em.writeln("logout") self.em = None self.transport.disconnect() self.transport = None
def disconnect(self): """ Disconnect from the element """ if self.transport: log.debug("------------------- disconnect() -------------------") self.em.writeln("logout") self.em = None self.transport.disconnect() self.transport = None
def save_running_config(self, callback=None): """ Store current-config as startup-config status: Todo """ log.debug("------------------- save_running_config() -------------------") if callback: callback("Save current-config as startup-config, hostname %s" % self.hostname) self.run("write") return True
def disconnect(self): """ Disconnect from the element """ if self.transport: log.debug("------------------- disconnect() -------------------") self.em.writeln("quit") self.em = None self.transport.disconnect() self.transport = None
def run(self, cmd=None, filter_=None, timeout=None, callback=None): """ Run a command on element returns a list with configuration lines, optionally filtering lines with a regex """ self.connect() log.debug("------------------- run() -------------------") self.em.writeln(cmd) self.wait_for_prompt() output = self.em.before.split("\r\n") return self.filter_(output, filter_)
def run(self, cmd=None, filter_=None, timeout=None, callback=None): """ Run a command on element returns a list with configuration lines, optionally filtering lines with a regex """ self.connect() log.debug("------------------- run() -------------------") self.em.writeln(cmd) self.wait_for_prompt() output = self.em.before.split("\r\n") return self.filter_(output, filter_)
def sw_copy_to(self, mgr=None, filename=None, dest_filename="bootflash:", callback=None): """ Copy file to element status: Todo """ self.connect() log.debug("------------------- sw_copy_to() -------------------") if callback: callback("Copy file %s to element" % (filename)) if self.swExist(filename): return # already on device. verify checksum? cmd = "copy %s/%s %s" % (mgr, filename, dest_filename) print("cmd " + cmd) self.em.writeln(cmd) match = self.em.expect(r"Destination filename.*\?") if match is None: raise self.ElementException("Unexpected output %s" % self.em.match) self.em.writeln("") match = self.em.expect({ "accessing": r"Accessing.*\r\n", "overwrite": r"Do you want to over write\? \[confirm\]", }) if match is None: raise self.ElementException("File transfer did not start") if match == "overwrite": self.em.writeln("y") block = 0 while True: block += 1 if callback: callback("Copying file to element, block %s" % block) match = self.em.expect({ "copying": r'!', "done": r'bytes copied', "error": r"%Error.*\r\n"}) if match is None: raise self.ElementException("File transfer finished incorrect, self.before=%s" % self.em.before ) if match == "copying": if callback is not None: callback(block) else: print("!", end="") continue elif match == "done": if callback: callback("Copying done, copied %s block" % block) break elif match == "error": raise self.ElementException("File transfer did not start. search buffer: %s" % self.em.before) self.wait_for_prompt()
def sw_copy_to(self, mgr=None, filename=None, dest_filename="bootflash:", callback=None): """ Copy file to element status: Todo """ self.connect() log.debug("------------------- sw_copy_to() -------------------") if callback: callback("Copy file %s to element" % (filename)) if self.swExist(filename): return # already on device. verify checksum? cmd = "copy %s/%s %s" % (mgr, filename, dest_filename) print("cmd " + cmd) self.em.writeln(cmd) match = self.em.expect(r"Destination filename.*\?") if match is None: raise self.ElementException("Unexpected output %s" % self.em.match) self.em.writeln("") match = self.em.expect({ "accessing": r"Accessing.*\r\n", "overwrite": r"Do you want to over write\? \[confirm\]", }) if match is None: raise self.ElementException("File transfer did not start") if match == "overwrite": self.em.writeln("y") block = 0 while True: block += 1 if callback: callback("Copying file to element, block %s" % block) match = self.em.expect({ "copying": r'!', "done": r'bytes copied', "error": r"%Error.*\r\n"}) if match is None: raise self.ElementException("File transfer finished incorrect, self.before=%s" % self.em.before ) if match == "copying": if callback is not None: callback(block) else: print("!", end="") continue elif match == "done": if callback: callback("Copying done, copied %s block" % block) break elif match == "error": raise self.ElementException("File transfer did not start. search buffer: %s" % self.em.before) self.wait_for_prompt()
def save_running_config(self, callback=None): """ Store current-config as startup-config status: Todo """ log.debug( "------------------- save_running_config() -------------------") if callback: callback("Save current-config as startup-config, hostname %s" % self.hostname) self.run("write") return True
def connect(self): log.debug( "------------------- connect(%s, use_ssh=%s) -------------------" % (self.hostname, self.use_ssh)) try: self.transport.connect(self.hostname, port=self.port, username=self.username, password=self.password) except comm.CommException as err: raise self.ElementException(err) self.em = comm.Expect(self.transport)
def sw_upgrade(self, mgr=None, filename=None, setboot=True, callback=None): """ Helper function. Uploads filename, set filename to boot, save running-config status: Todo """ raise self.ElementException("Not implemented") log.debug("------------------- sw_upgrade() -------------------") if not self.sw_exist(filename): self.swCopyTo(mgr=mgr, filename=filename, callback=callback) if setboot: self.sw_set_boot(filename) self.save_running_config(callback=callback)
def sw_upgrade(self, mgr=None, filename=None, setboot=True, callback=None): """ Helper function. Uploads filename, set filename to boot, save running-config status: Todo """ raise self.ElementException("Not implemented") log.debug("------------------- sw_upgrade() -------------------") if not self.sw_exist(filename): self.swCopyTo(mgr=mgr, filename=filename, callback=callback) if setboot: self.sw_set_boot(filename) self.save_running_config(callback=callback)
def save_running_config(self, callback=None): """ Store current-config as startup-config status: Todo """ self.connect() log.debug("------------------- save_running_config() -------------------") if callback: callback("Save current-config as startup-config, hostname %s" % self.hostname) # self.run("write") self.em.writeln("write") match = self.em.expect("Save Config Succeeded!") self.wait_for_prompt() return True
def sw_delete_unneeded(self, callback=None): """ Delete unneeded firmware We keep the one pointed to by "boot system flash" and the currently running one """ self.connect() keep = {} bootflash = {} deleted = [] files = self.sw_list() # 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.sw_delete(f) if f in bootflash: self.configure("no " + bootflash[f]) return deleted
def run(self): try: super().run() for cmd in self.args.command: log.debug("cmd: %s" % cmd) lines = self.mgr.run(cmd=cmd) if lines: ix = 0 for line in lines: #print("%d %s" % (ix, line)) print("%s" % (line)) ix += 1 except self.mgr_cls.ElementException as err: print("Error: %s" % err)
def save_running_config(self, callback=None): """ Store current-config as startup-config status: Todo """ self.connect() log.debug( "------------------- save_running_config() -------------------") if callback: callback("Save current-config as startup-config, hostname %s" % self.hostname) self.run("save") match = self.em.expect("configuration successfully") self.wait_for_prompt() return True
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it status: Todo """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reboot") self.em.expect("rebooting") # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure terminal") self.wait_for_prompt() for config_line in config_lines: self.em.writeln(config_line) self.em.writeln("end") self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure terminal") self.wait_for_prompt() for config_line in config_lines: self.em.writeln(config_line) self.em.writeln("end") self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it status: Todo """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reboot") self.em.expect("rebooting") # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reload") match = self.em.expect(r"\[y/N\]\:") self.em.writeln("y") time.sleep(2) # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device Raycore has no configuration mode, everything is commands, as in run """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure") self.wait_for_prompt() for config_line in config_lines: if config_line: self.em.writeln(config_line) self.wait_for_prompt() # if save_running_config: # self.save_running_config() return True
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reload") match = self.em.expect(r"\[y/N\]\:") self.em.writeln("y") time.sleep(2) # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def sw_delete_unneeded(self, callback=None): """ Delete unneeded firmware, so we have room for installing a new one This method keeps the one specified to boot """ bootimg = self.sw_get_boot() files = self.sw_list() deleted = [] if bootimg not in files: raise self.ElementException("Boot image does not exist in flash") for f in files: if f != bootimg: log.debug("Deleting file %s" % f) deleted.append(f) self.sw_delete(f) return deleted
def sw_delete_unneeded(self, callback=None): """ Delete unneeded firmware, so we have room for installing a new one This method keeps the one specified to boot """ bootimg = self.sw_get_boot() files = self.sw_list() deleted = [] if bootimg not in files: raise self.ElementException("Boot image does not exist in flash") for f in files: if f != bootimg: log.debug("Deleting file %s" % f) deleted.append(f) self.sw_delete(f) return deleted
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("system") match = self.wait_for_prompt() if match is None: raise self.ElementException("Error Could not enter configuration mode") for config_line in config_lines: self.em.writeln(config_line) self.em.writeln("return") self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure") match = self.wait_for_prompt() if match is None: raise self.ElementException("Error Could not enter configuration mode") for config_line in config_lines: self.em.writeln(config_line) self.wait_for_prompt() self.em.writeln("exit") # Todo, multiple exit until (config) disappears from prompt? self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def get_running_config(self, filter_=None, refresh=False, callback=None): """ Get config lines from running-config, optionally filtering with a regex returns a list """ log.debug("------------------- get_running_config() -------------------") if not refresh and self.running_config is None: self.running_config = self.run("show running-config") if filter_ is None: return self.running_config # filter out the lines matching match = [] p = re.compile(filter_) for line in self.running_config: if p.search(line): match.append(line) return match
def expect(self, matches, timeout=20): """ Wait until match or timeout If match, returns key of which regex matched if no match (timeout), returns None """ self.before = '' self.match = None if isinstance(matches, str): matches = {'0': matches} elif isinstance(matches, list): tmp = {} ix = 0 for match in matches: tmp[ix] = match ix += 1 matches = tmp regexes = {} for key, match in matches.items(): regexes[key] = re.compile(match) while True: c = self.transport.read(timeout=timeout) if c is None: break self.before += c for key, regex in regexes.items(): m = regex.search(self.before) if m: log.debug(" expect, matched pattern: '%s' %s" % (key, regex)) self.match = m.group() if log.isEnabledFor(log.DEBUG): tmp = self.match.replace("\n", "\\n").replace("\r", "\\r") log.debug(" expect, matched text : %s" % tmp) tmp = self.before[:m.end()] # Everthing up to matched text if log.isEnabledFor(log.DEBUG): tmp = tmp.replace("\n", "\\n").replace("\r", "\\r") log.debug(" expect, self.before : %s" % tmp) tmp = self.before[m.end():] if len(tmp): # There are received data after our match, return the extra data if log.isEnabledFor(log.DEBUG): tmp = tmp.replace("\n", "\\n").replace("\r", "\\r") log.debug(" expect, returned to buffer: '%s'" % tmp) self.transport.unread(self.before[m.end():]) # text after match is returned to transport return key raise CommException(1, " expect, timeout, self.before: %s" % self.before)
def get_running_config(self, filter_=None, refresh=False, callback=None): """ Get config lines from running-config, optionally filtering with a regex returns a list """ log.debug("------------------- get_running_config() -------------------") if not refresh and self.running_config is None: self.running_config = self.run("show running-config") if filter_ is None: return self.running_config # filter out the lines matching match = [] p = re.compile(filter_) for line in self.running_config: if p.search(line): match.append(line) return match
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it status: Todo """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reload") match = self.em.expect({ "verify": r"re you sure to reset the board?[yes/no]:" }) if match is "verify": self.em.writeln("yes") # self.em.expect("rebooting") # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def reload(self, save_config=True, callback=None): """ Reload the element. If running-config is unsaved, option to save it status: Todo """ self.connect() log.debug("------------------- reload() -------------------") if save_config: self.save_running_config() self.em.writeln("reload") match = self.em.expect( {"verify": r"re you sure to reset the board?[yes/no]:"}) if match is "verify": self.em.writeln("yes") # self.em.expect("rebooting") # Force the connection closed self.em = None self.transport.disconnect() self.transport = None
def configure(self, config_lines=None, save_running_config=False, callback=None): """ Reconfigure device todo: trigger on '%-ERR: <error description>' """ self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure terminal") match = self.em.expect("\(config\)#") if match is None: raise self.ElementException("Error Could not enter configuration mode") for config_line in config_lines: self.em.writeln(config_line) match = self.em.expect("\)#") if match is None: raise self.ElementException("Error waiting for next configuration prompt") self.em.writeln("end") self.wait_for_prompt() if save_running_config: self.save_running_config() return True
def configure(self, config_lines, save_running_config=False, callback=None): """ Reconfigure device """ ret = [] self.connect() log.debug("------------------- configure() -------------------") config_lines = self.str_to_lines(config_lines) self.em.writeln("configure") self.wait_for_prompt() for config_line in config_lines: self.em.writeln(config_line) ret = self.wait_for_prompt() if ret: ret += self.em.before # time.sleep(1) self.em.writeln("end") self.wait_for_prompt() if save_running_config: self.save_running_config() return ret
def sw_set_boot(self, filename, callback=None): """ Check if filename exist in element if true configure element to boot with the filename status: Todo """ raise self.ElementException("Not implemented") self.connect() log.debug("------------------- sw_set_boot() -------------------") if not self.sw_exist(filename): raise self.ElementException("Error cant change boot software, filename %s does not exist" % filename) # remove old boot system flash commands # todo # startup system-software S5300EI-V200R003C00SPC300.cc lines = self.get_running_config(filter_="^boot system flash ") for line in lines[1:]: print(" no " + line) self.setConfig("no " + line) # set new boot system flash cmd = "boot system flash %s" % filename self.setConfig(cmd) self.wait_for_prompt()
def expect(self, matches, timeout=20): """ Wait until match or timeout If match, returns key of which regex matched if no match (timeout), returns None """ self.before = '' self.match = None if isinstance(matches, str): matches = {'0': matches} elif isinstance(matches, list): tmp = {} ix = 0 for match in matches: tmp[ix] = match ix += 1 matches = tmp regexes = {} for key, match in matches.items(): regexes[key] = re.compile(match) log.debug("expect, match criteria %s" % regexes) while True: c = self.transport.read(timeout=timeout) if c is None: break self.before += c for key, regex in regexes.items(): m = regex.search(self.before) if m: self.match = m.group() log.debug(" expect, matched: %s" % self.match) tmp = self.before[:m.end()] # Everthing up to matched text tmp = tmp.replace("\n", "\\n") tmp = tmp.replace("\r", "\\r") log.debug(" expect, self.before: %s" % tmp) log.debug(" expect, returned to buffer: '%s'" % self.before[m.end():]) self.transport.unread(self.before[m.end():]) # text after match is returned to transport return key raise CommException(1, " expect, timeout, self.before: %s" % self.before)
def writeln(self, msg=None): log.debug("expect writeln: '%s'" % msg) self.transport.writeln(msg)
def write(self, msg): log.debug("expect write: %s" % msg) self.transport.write(msg)
def writeln(self, msg=None): if msg == None: msg = "" log.debug("------------------- writeln('%s') -------------------" % msg) self.transport.writeln(msg)