Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to stop executing commands that take too long #954

Open
BadKid90s opened this issue Jul 31, 2024 · 0 comments
Open

How to stop executing commands that take too long #954

BadKid90s opened this issue Jul 31, 2024 · 0 comments

Comments

@BadKid90s
Copy link

try {
         sshClient = new SSHClient();
         sshClient.addHostKeyVerifier(new PromiscuousVerifier());
         sshClient.connect(host, port); // 连接到远程主机
         sshClient.authPassword(username, password); // 使用密码进行身份验证
         sshClient.setTimeout(timeout);
         sshClient.setConnectTimeout(timeout);
     } catch (IOException e) {
         throw new RuntimeException(e);
     }
 try {

            Session.Command cmd = session.exec(cmdStr);

            // 获取标准输出
            String output = IOUtils.readFully(cmd.getInputStream()).toString();
            log.debug("execute command output: {}", output);

            // 获取错误输出
            String errorOutput = IOUtils.readFully(cmd.getErrorStream()).toString();
            if (errorOutput != null && !errorOutput.isEmpty()) {
                log.debug("execute command error output: {}", output);
                throw new CommandExecuteException(errorOutput);
            }

            cmd.join(this.timeout, TimeUnit.SECONDS);

            session.close();
            return output;

        } catch (IOException e) {
            throw new CommandExecuteException(e.getMessage());
        }

How to stop executing commands that take too long???

 sshClient.setTimeout(timeout);   
 sshClient.setConnectTimeout(timeout);    
 cmd.join(this.timeout, TimeUnit.SECONDS);  

None of them have taken effect

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant