diff --git a/activate_boot.py b/activate_boot.py index ab7f52a..8779849 100644 --- a/activate_boot.py +++ b/activate_boot.py @@ -80,7 +80,8 @@ def uboot_boot_change(gw, fw_num): cmd.append("nvram set flag_try_sys2_failed=0") cmd.append("nvram set flag_boot_rootfs={}".format(fw_num)) cmd.append("nvram commit") - return gw.run_cmd(';'.join(cmd)) + out = gw.run_cmd(';'.join(cmd)) + return False if out is None else True if __name__ == "__main__": diff --git a/create_backup.py b/create_backup.py index 3d80696..75c2d4a 100644 --- a/create_backup.py +++ b/create_backup.py @@ -77,7 +77,7 @@ def backup_and_download(pid, filename, chunk_size = 0, die_on_error = True): count = max_blocks cmd = f"rm -f {fn_remote} ; dd if=/dev/mtd{pid} of={fn_remote} bs={blk_size} skip={skip} count={count}" ret = gw.run_cmd(cmd, timeout = 25, die_on_error = False) - if not ret: + if ret is None: print(f'ERROR on execute command: "{cmd}"') if die_on_error: sys.exit(1) diff --git a/gateway.py b/gateway.py index ea8dcee..3da2be2 100644 --- a/gateway.py +++ b/gateway.py @@ -894,19 +894,21 @@ class Gateway(): return False return True - def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True): - ret = True + def run_cmd(self, command, msg = None, timeout = None, die_on_error = True): + error = 0 if self.use_ssh: ssh = self.get_ssh(self.verbose) else: tn = self.get_telnet(self.verbose) if (msg): print(msg) - cmdlist = [] - if isinstance(cmd, str): - cmdlist.append(cmd) + cmdlist = [ ] + if isinstance(command, str): + cmdlist.append(command) else: - cmdlist = cmd + cmdlist = command + if not cmdlist: + raise ValueError('Incorrect command list') for idx, cmd in enumerate(cmdlist): if self.use_ssh: channel = ssh.open_session() @@ -920,7 +922,7 @@ class Gateway(): channel.wait_eof() except ssh2.exceptions.Timeout: ssh.set_timeout(100) - ret = False + error = -4 if die_on_error: die("SSH execute command timed out! CMD: \"{}\"".format(cmd)) if timeout is not None: @@ -931,16 +933,16 @@ class Gateway(): except Exception: pass #status = channel.get_exit_status() - if not ret: - break - else: + else: # telnet cmd += '\n' tn.write(cmd.encode('ascii')) tn.read_until(tn.prompt, timeout = 4 if timeout is None else timeout) + if error != 0: + break if not self.use_ssh: tn.write(b"exit\n") - ret = True - return ret + tn.close() + return True if error == 0 else None def download(self, fn_remote, fn_local, verbose = 1): if verbose and self.verbose: @@ -1008,7 +1010,7 @@ class Gateway(): md5_remote_fn = f"/tmp/{fname}.{num}.md5" cmd = f'md5sum "{fn_remote}" > "{md5_remote_fn}" 2>&1' rc = self.run_cmd(cmd, timeout = timeout) - if not rc: + if rc is None: return -5 os.remove(md5_local_fn) if os.path.exists(md5_local_fn) else None self.download(md5_remote_fn, md5_local_fn, verbose = 0) diff --git a/install_fw.py b/install_fw.py index 4aa6705..c3226f3 100644 --- a/install_fw.py +++ b/install_fw.py @@ -1079,7 +1079,7 @@ class XqFlash(): cmd.append("nvram set uart_en=1") cmd.append("nvram commit") rc = gw.run_cmd(';'.join(cmd), timeout = 8) - if not rc: + if rc is None: die(f'Cannot change nvram parameters!') if fw_img.cmd: @@ -1133,7 +1133,7 @@ class XqFlash(): if not self.img_write: return True rc = self.gw.run_cmd(img.cmd, timeout = timeout, die_on_error = True) - if not rc: + if rc is None: print(f' ERROR: cannot flash data to partition "{partname}"') return False if check: