From 97d7d6836d1419c77a1f7de674ae16c811a7a6ad Mon Sep 17 00:00:00 2001 From: remittor Date: Wed, 5 Jan 2022 21:24:54 +0300 Subject: [PATCH] Several small edits --- connect2.py | 2 +- create_backup.py | 21 +++++++++++++-------- gateway.py | 14 +++++++++++--- read_info.py | 13 +++++++------ 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/connect2.py b/connect2.py index b01e36a..00d4cfe 100644 --- a/connect2.py +++ b/connect2.py @@ -46,7 +46,7 @@ def exec_cmd(cmd): res = requests.get(gw.apiurl + ext_name, params = params) return res.text -res = exec_cmd('nvram set bootdelay=5; nvram set ssh_en=1; nvram commit;') +res = exec_cmd('nvram set bootdelay=3; set boot_wait=on; nvram set ssh_en=1; nvram commit;') if res != '{"code":0}': die('Extension "/api/{}" not working!'.format(ext_name)) diff --git a/create_backup.py b/create_backup.py index e643eb0..a96ee3c 100644 --- a/create_backup.py +++ b/create_backup.py @@ -95,15 +95,20 @@ else: if os.path.exists(fn_old): os.remove(fn_old) os.rename(fn_local, fn_old) - gw.run_cmd("dd if=/dev/mtd{id} of={o}".format(id=p, o=fn_remote)) - print('Download dump to file "./{}"...'.format(fn_local)) - try: - gw.download(fn_remote, fn_local, verbose = 0) - except Exception: - print('Remote file "{}" not found!'.format(fn_remote)) - continue + cmd = "dd if=/dev/mtd{id} of={o}".format(id=p, o=fn_remote) + ret = gw.run_cmd(cmd, timeout=30, die_on_error = False) + if not ret: + print('ERROR on execute command: "{}"'.format(cmd)) + else: + print('Download dump to file "./{}"...'.format(fn_local)) + try: + gw.download(fn_remote, fn_local, verbose = 0) + except Exception: + print('Remote file "{}" not found!'.format(fn_remote)) + continue gw.run_cmd("rm -f " + fn_remote) - print('Backup of "{}" saved to file "./{}"'.format(name, fn_local)) + if ret: + print('Backup of "{}" saved to file "./{}"'.format(name, fn_local)) print(" ") print("Completed!") diff --git a/gateway.py b/gateway.py index 7607f76..de3b9cb 100644 --- a/gateway.py +++ b/gateway.py @@ -14,6 +14,8 @@ import requests import atexit import socket + +sys.path.append(os.path.dirname(os.path.abspath(__file__))) import ssh2 from ssh2.error_codes import LIBSSH2_ERROR_EAGAIN from ssh2.utils import wait_socket @@ -598,7 +600,8 @@ class Gateway(): return False return True - def run_cmd(self, cmd, msg = None, timeout = None): + def run_cmd(self, cmd, msg = None, timeout = None, die_on_error = True): + ret = True if self.use_ssh: ssh = self.get_ssh(self.verbose) else: @@ -622,7 +625,10 @@ class Gateway(): try: channel.wait_eof() except ssh2.exceptions.Timeout: - die("SSH execute command timed out! CMD: \"{}\"".format(cmd)) + ssh.set_timeout(100) + ret = False + if die_on_error: + die("SSH execute command timed out! CMD: \"{}\"".format(cmd)) if timeout is not None: ssh.set_timeout(saved_timeout) try: @@ -631,13 +637,15 @@ class Gateway(): except Exception: pass #status = channel.get_exit_status() + if not ret: + break else: cmd = (cmd + '\n').encode('ascii') tn.write(cmd) tn.read_until(tn.prompt, timeout = 4 if timeout is None else timeout) if not self.use_ssh: tn.write(b"exit\n") - return True + return ret def download(self, fn_remote, fn_local, verbose = 1): if verbose and self.verbose: diff --git a/read_info.py b/read_info.py index 0dc2d28..25ab0e4 100644 --- a/read_info.py +++ b/read_info.py @@ -636,6 +636,7 @@ class SysLog(): verbose = 1 timeout = 10 files = [] + skiplogs = True mtdlist = [] bdata = None # EnvBuffer() @@ -673,7 +674,7 @@ class SysLog(): die("Xiaomi Mi Wi-Fi device not found (IP: {})".format(gw.ip_addr)) if self.verbose > 0: print("Start generating syslog...") - r2 = requests.get(gw.apiurl + "misystem/sys_log", timeout = self.timeout) + r2 = requests.get(gw.apiurl + "misystem/sys_log", timeout = timeout) if r2.text.find('"code":0') < 0: die("SysLog not generated!") try: @@ -685,7 +686,7 @@ class SysLog(): if self.verbose > 0: print('Downloading SysLog from file "{}" ...'.format(url)) zip = b'' - with requests.get(url, stream=True, timeout = self.timeout) as r3: + with requests.get(url, stream=True, timeout = timeout) as r3: r3.raise_for_status() for chunk in r3.iter_content(chunk_size=8192): zip += chunk @@ -698,7 +699,7 @@ class SysLog(): for member in tar.getmembers(): if not member.isfile() or not member.name: continue - if member.name.find('usr/log/') >= 0: # skip raw syslog files + if self.skiplogs and member.name.find('usr/log/') >= 0: # skip raw syslog files continue item = types.SimpleNamespace() item.name = member.name @@ -724,7 +725,7 @@ class SysLog(): def parse_baseinfo(self, fatal_error = False): self.device_sn = "" file = self.get_file_by_name('xiaoqiang.log', fatal_error) - txt = file.data.decode('ascii') + txt = file.data.decode('latin_1') sn = re.search('====SN\n(.*?)\n====', txt) if not sn: if fatal_error: @@ -739,7 +740,7 @@ class SysLog(): def parse_mtdlist(self): self.mtdlist = [] file = self.get_file_by_name('xiaoqiang.log', fatal_error = True) - txt = file.data.decode('ascii') + txt = file.data.decode('latin_1') x = txt.find("\nMTD table:\n") if x <= 0: die('MTD table not found into syslog!') @@ -788,7 +789,7 @@ if __name__ == "__main__": gw = gateway.Gateway(timeout = 4, detect_ssh = False) if gw.status < 1: die("Xiaomi Mi Wi-Fi device not found (IP: {})".format(gw.ip_addr)) - slog = SysLog(gw, timeout = 17, verbose = 1, infolevel = 2) + slog = SysLog(gw, timeout = 22, verbose = 1, infolevel = 2) sys.exit(0) fn_dir = ''