sshj
sshj copied to clipboard
How to stop executing commands that take too long
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