From 54d848e11bc83ff28710abc7aff21a8dc6787510 Mon Sep 17 00:00:00 2001 From: Simon Lin Date: Tue, 14 Dec 2021 15:43:11 +0800 Subject: [PATCH] [otci] add method to run shell commands (#7230) --- tools/otci/otci/command_handlers.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tools/otci/otci/command_handlers.py b/tools/otci/otci/command_handlers.py index 3c50ddd89..ee0b5f3eb 100644 --- a/tools/otci/otci/command_handlers.py +++ b/tools/otci/otci/command_handlers.py @@ -76,6 +76,9 @@ class OTCommandHandler: """ pass + def shell(self, cmd: str, timeout: float) -> List[str]: + raise NotImplementedError("shell command is not supported on %s" % self.__class__.__name__) + class OtCliCommandRunner(OTCommandHandler): __PATTERN_COMMAND_DONE_OR_ERROR = re.compile( @@ -224,16 +227,11 @@ class OtbrSshCommandRunner(OTCommandHandler): return f'{self.__host}:{self.__port}' def execute_command(self, cmd: str, timeout: float) -> List[str]: - sh_cmd = f'ot-ctl -- {cmd}' + sh_cmd = f'ot-ctl {cmd}' if self.__sudo: sh_cmd = 'sudo ' + sh_cmd - cmd_in, cmd_out, cmd_err = self.__ssh.exec_command(sh_cmd, timeout=int(timeout), bufsize=1024) - err = cmd_err.read().decode('utf-8') - if err: - raise CommandError(cmd, [err]) - - output = [l.rstrip('\r\n') for l in cmd_out.readlines()] + output = self.shell(sh_cmd, timeout=timeout) if self.__line_read_callback is not None: for line in output: @@ -244,6 +242,16 @@ class OtbrSshCommandRunner(OTCommandHandler): return output + def shell(self, cmd: str, timeout: float) -> List[str]: + cmd_in, cmd_out, cmd_err = self.__ssh.exec_command(cmd, timeout=int(timeout), bufsize=1024) + errput = [l.rstrip('\r\n') for l in cmd_err.readlines()] + output = [l.rstrip('\r\n') for l in cmd_out.readlines()] + + if errput: + raise CommandError(cmd, errput) + + return output + def close(self): self.__ssh.close()