def execute_on_target(self, commandToExecute, block=True, env={}, cwd=None, timeout=None): prefix = helper.get_env_var_setting_string(env) command = "{} ".format(prefix) + commandToExecute if cwd: command = "cd {}; ".format(cwd) + command log.info("[{}]{}".format(self.name, command)) stdin, stdout, stderr = self.sshClient.exec_command(command) stdoutBuffer = Buffer() stdoutReader = AsynchronousPipeReader(stdout, stdoutBuffer) stderrBuffer = Buffer() stderrReader = AsynchronousPipeReader(stderr, stderrBuffer) if block: if timeout: # poll on application exit with timeout to prevent infinite blocking endTime = time.time() + timeout while time.time( ) < endTime and not stdout.channel.exit_status_ready(): time.sleep(0.1) if not stdout.channel.exit_status_ready(): return [], ["<timeout>"], 1 # get application exit status blocking. will immediately succeed when timeout was given returnCode = stdout.channel.recv_exit_status() stdoutReader.stop(withTimeout=True) stderrReader.stop(withTimeout=True) return stdoutBuffer.get_all_data(), stderrBuffer.get_all_data( ), returnCode return None
def execute_on_target(self, commandToExecute, block=True, env={}, cwd=None): prefix = helper.get_env_var_setting_string(env) command = "{} ".format(prefix) + commandToExecute if cwd: command = "cd {}; ".format(cwd) + command log.info("executing '" + command + "' on target") stdin, stdout, stderr = self.sshClient.exec_command(command) stdoutBuffer = Buffer() stdoutReader = AsynchronousPipeReader(stdout, stdoutBuffer) stderrBuffer = Buffer() stderrReader = AsynchronousPipeReader(stderr, stderrBuffer) if (block): returnCode = stdout.channel.recv_exit_status( ) #block till call is finished stdoutReader.stop(withTimeout=True) stderrReader.stop(withTimeout=True) return stdoutBuffer.get_all_data(), stderrBuffer.get_all_data( ), returnCode return None