mirror of
https://github.com/openwrt-xiaomi/xmir-patcher.git
synced 2026-09-22 22:57:55 +03:00
gateway: Change usage of run_cmd function
This commit is contained in:
+2
-1
@@ -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_try_sys2_failed=0")
|
||||||
cmd.append("nvram set flag_boot_rootfs={}".format(fw_num))
|
cmd.append("nvram set flag_boot_rootfs={}".format(fw_num))
|
||||||
cmd.append("nvram commit")
|
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__":
|
if __name__ == "__main__":
|
||||||
|
|||||||
+1
-1
@@ -77,7 +77,7 @@ def backup_and_download(pid, filename, chunk_size = 0, die_on_error = True):
|
|||||||
count = max_blocks
|
count = max_blocks
|
||||||
cmd = f"rm -f {fn_remote} ; dd if=/dev/mtd{pid} of={fn_remote} bs={blk_size} skip={skip} count={count}"
|
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)
|
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}"')
|
print(f'ERROR on execute command: "{cmd}"')
|
||||||
if die_on_error:
|
if die_on_error:
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|||||||
+15
-13
@@ -894,19 +894,21 @@ class Gateway():
|
|||||||
return False
|
return False
|
||||||
return True
|
return True
|
||||||
|
|
||||||
def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True):
|
def run_cmd(self, command, msg = None, timeout = None, die_on_error = True):
|
||||||
ret = True
|
error = 0
|
||||||
if self.use_ssh:
|
if self.use_ssh:
|
||||||
ssh = self.get_ssh(self.verbose)
|
ssh = self.get_ssh(self.verbose)
|
||||||
else:
|
else:
|
||||||
tn = self.get_telnet(self.verbose)
|
tn = self.get_telnet(self.verbose)
|
||||||
if (msg):
|
if (msg):
|
||||||
print(msg)
|
print(msg)
|
||||||
cmdlist = []
|
cmdlist = [ ]
|
||||||
if isinstance(cmd, str):
|
if isinstance(command, str):
|
||||||
cmdlist.append(cmd)
|
cmdlist.append(command)
|
||||||
else:
|
else:
|
||||||
cmdlist = cmd
|
cmdlist = command
|
||||||
|
if not cmdlist:
|
||||||
|
raise ValueError('Incorrect command list')
|
||||||
for idx, cmd in enumerate(cmdlist):
|
for idx, cmd in enumerate(cmdlist):
|
||||||
if self.use_ssh:
|
if self.use_ssh:
|
||||||
channel = ssh.open_session()
|
channel = ssh.open_session()
|
||||||
@@ -920,7 +922,7 @@ class Gateway():
|
|||||||
channel.wait_eof()
|
channel.wait_eof()
|
||||||
except ssh2.exceptions.Timeout:
|
except ssh2.exceptions.Timeout:
|
||||||
ssh.set_timeout(100)
|
ssh.set_timeout(100)
|
||||||
ret = False
|
error = -4
|
||||||
if die_on_error:
|
if die_on_error:
|
||||||
die("SSH execute command timed out! CMD: \"{}\"".format(cmd))
|
die("SSH execute command timed out! CMD: \"{}\"".format(cmd))
|
||||||
if timeout is not None:
|
if timeout is not None:
|
||||||
@@ -931,16 +933,16 @@ class Gateway():
|
|||||||
except Exception:
|
except Exception:
|
||||||
pass
|
pass
|
||||||
#status = channel.get_exit_status()
|
#status = channel.get_exit_status()
|
||||||
if not ret:
|
else: # telnet
|
||||||
break
|
|
||||||
else:
|
|
||||||
cmd += '\n'
|
cmd += '\n'
|
||||||
tn.write(cmd.encode('ascii'))
|
tn.write(cmd.encode('ascii'))
|
||||||
tn.read_until(tn.prompt, timeout = 4 if timeout is None else timeout)
|
tn.read_until(tn.prompt, timeout = 4 if timeout is None else timeout)
|
||||||
|
if error != 0:
|
||||||
|
break
|
||||||
if not self.use_ssh:
|
if not self.use_ssh:
|
||||||
tn.write(b"exit\n")
|
tn.write(b"exit\n")
|
||||||
ret = True
|
tn.close()
|
||||||
return ret
|
return True if error == 0 else None
|
||||||
|
|
||||||
def download(self, fn_remote, fn_local, verbose = 1):
|
def download(self, fn_remote, fn_local, verbose = 1):
|
||||||
if verbose and self.verbose:
|
if verbose and self.verbose:
|
||||||
@@ -1008,7 +1010,7 @@ class Gateway():
|
|||||||
md5_remote_fn = f"/tmp/{fname}.{num}.md5"
|
md5_remote_fn = f"/tmp/{fname}.{num}.md5"
|
||||||
cmd = f'md5sum "{fn_remote}" > "{md5_remote_fn}" 2>&1'
|
cmd = f'md5sum "{fn_remote}" > "{md5_remote_fn}" 2>&1'
|
||||||
rc = self.run_cmd(cmd, timeout = timeout)
|
rc = self.run_cmd(cmd, timeout = timeout)
|
||||||
if not rc:
|
if rc is None:
|
||||||
return -5
|
return -5
|
||||||
os.remove(md5_local_fn) if os.path.exists(md5_local_fn) else None
|
os.remove(md5_local_fn) if os.path.exists(md5_local_fn) else None
|
||||||
self.download(md5_remote_fn, md5_local_fn, verbose = 0)
|
self.download(md5_remote_fn, md5_local_fn, verbose = 0)
|
||||||
|
|||||||
+2
-2
@@ -1079,7 +1079,7 @@ class XqFlash():
|
|||||||
cmd.append("nvram set uart_en=1")
|
cmd.append("nvram set uart_en=1")
|
||||||
cmd.append("nvram commit")
|
cmd.append("nvram commit")
|
||||||
rc = gw.run_cmd(';'.join(cmd), timeout = 8)
|
rc = gw.run_cmd(';'.join(cmd), timeout = 8)
|
||||||
if not rc:
|
if rc is None:
|
||||||
die(f'Cannot change nvram parameters!')
|
die(f'Cannot change nvram parameters!')
|
||||||
|
|
||||||
if fw_img.cmd:
|
if fw_img.cmd:
|
||||||
@@ -1133,7 +1133,7 @@ class XqFlash():
|
|||||||
if not self.img_write:
|
if not self.img_write:
|
||||||
return True
|
return True
|
||||||
rc = self.gw.run_cmd(img.cmd, timeout = timeout, die_on_error = 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}"')
|
print(f' ERROR: cannot flash data to partition "{partname}"')
|
||||||
return False
|
return False
|
||||||
if check:
|
if check:
|
||||||
|
|||||||
Reference in New Issue
Block a user