java - 通过 SSH 连接服务器时出现异常

标签 java exception jakarta-ee ssh solaris

我正在尝试在 Java 应用程序中使用 SSH 从 Solaris 服务器连接到外部服务器。一些我们在验证用户身份时但在 60 分钟后遇到异常的情况。我们如何才能将异常时间减少到 5 分钟左右。

连接 IP:PORT 时出现问题 java.io.IOException:连接到 IP:PORT 时出现问题

时间2和时间3之间的时间差约为60分钟。这次我想减少。

请在下面找到我们正在使用的代码片段。

try
{
    timeout = 1;
    if (connection == null)
    {
        //time1
        connection = new ch.ethz.ssh2.Connection(getIpAddress(), Integer.parseInt(getPort()));
        if (connection == null)
        {
            System.out.println("connection is null");
        }
        else
        {
            //time2
            connection.connect(null, timeout, 0);
        }
    }
}
catch (Exception t)
{
    //time3
    System.out.println(t.getLocalizedMessage());
    System.out.println(t.toString());
}

Edit1:检查SSH相关配置文件后,我发现KeyRegenesisInterval的值为3600秒。这对解决这个问题有用吗?如果我将其值减少到大约 30 分钟或 5 分钟,可能会出现什么结果。

最佳答案

让我总结一下您的问题,如果有问题请纠正。

  1. 您无法连接到 SSH 服务器。
  2. 60 分钟后引发异常。

根据下面的方法的抛出部分connect(ServerHostKeyVerifier, int, int) ,我猜您可能会遇到由于代理错误而无法返回正确 HTTP 响应的问题。使用直接互联网连接来查看问题是否消失。

Throws:

java.io.IOException - If any problem occurs, e.g., the server's host key is not accepted by the verifier or there is problem during the initial crypto setup (e.g., the signature sent by the server is wrong).

In case of a timeout (either connectTimeout or kexTimeout) a SocketTimeoutException is thrown.

An exception may also be thrown if the connection was already successfully connected (no matter if the connection broke in the mean time) and you invoke connect() again without having called close() first.

If a HTTP proxy is being used and the proxy refuses the connection, then a HTTPProxyException may be thrown, which contains the details returned by the proxy. If the proxy is buggy and does not return a proper HTTP response, then a normal IOException is thrown instead.

关于java - 通过 SSH 连接服务器时出现异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58795844/

相关文章:

java - 使用 Guava 的 ImmutableSortedMap 作为频率图

java - 尝试修改 Activity 的后退按钮

java - JTextPane/JTextField 罕见字符的奇怪行为

python - 我的 KeyboardInterrupt 仅在 90% 的情况下被捕获。我在什么方面失败了?

asp.net-mvc - ExceptionContext.ExceptionHandled 更改为 true。异常在哪里处理?

java - 从 Spring Controller 调用 servlet

java - Spring Boot 中的 ManyToMany 映射问题

java - 如何使用文本文件中的 PrinterWriter 类获得以下结果?

sql - 何时使用查询或代码

java - log4j2问题。无法在控制台或文件中记录任何内容