简体   繁体   English

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

[英]Getting exception while connecting a server through SSH

I am trying to connect to an external server from my Solaris server using SSH in my Java application.我正在尝试在我的 Java 应用程序中使用 SSH 从我的 Solaris 服务器连接到外部服务器。 Some how we are getting exception while authenticating the user but after 60min.一些我们在验证用户但在 60 分钟后遇到异常的方式。 How can we decrease the timing to get the exception to 5min or so.我们如何才能将获得异常的时间缩短到 5 分钟左右。

There was a problem while connecting to IP:PORT java.io.IOException: There was a problem while connecting to IP:PORT There was a problem while connecting to IP:PORT java.io.IOException: There was a problem while connecting to IP:PORT

The time difference between time2 and time3 are around 60min. time2 和 time3 之间的时间差在 60 分钟左右。 I want to decrease this time.我想减少这个时间。

Please find below the code snippet that we are using.请在下面找到我们正在使用的代码片段。

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: After checking SSH related configuration files I found KeyRegenerationInterval having a value of 3600s. Edit1:检查SSH相关配置文件后,我发现KeyRegenerationInterval的值为3600s。 Is this useful to resolve this issue.这对解决这个问题有用吗。 What might be the outcome if I decrease its value to some 30min or 5min.如果我将其值降低到大约 30 分钟或 5 分钟,可能会产生什么结果。

Let me summarize your problem, please correct me if something wrong.让我总结一下你的问题,如果有问题请纠正我。

  1. You can not connect to SSH server.您无法连接到 SSH 服务器。
  2. An exception raised after 60 mins. 60 分钟后引发异常。

According to below Throws section of method connect(ServerHostKeyVerifier, int, int) , I guess you might be facing issue due to a buggy proxy which doesn't return proper HTTP response.根据下面的方法connect(ServerHostKeyVerifier, int, int)的 Throws 部分,我猜你可能会因为一个错误的代理而面临问题,它没有返回正确的 HTTP 响应。 Using direct internet connection to see whether the issue is gone.使用直接互联网连接来查看问题是否消失。

Throws:抛出:

java.io.IOException - If any problem occurs, eg, the server's host key is not accepted by the verifier or there is problem during the initial crypto setup (eg, the signature sent by the server is wrong). java.io.IOException - 如果出现任何问题,例如,验证者不接受服务器的主机密钥或在初始加密设置期间出现问题(例如,服务器发送的签名错误)。

In case of a timeout (either connectTimeout or kexTimeout) a SocketTimeoutException is thrown.如果发生超时(connectTimeout 或 kexTimeout),则会引发 SocketTimeoutException。

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.如果连接已经成功连接(无论连接是否同时中断)并且您在没有先调用close()的情况下再次调用 connect() ,也可能会引发异常。

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.如果正在使用 HTTP 代理并且代理拒绝连接,则可能会抛出HTTPProxyException ,其中包含代理返回的详细信息。 If the proxy is buggy and does not return a proper HTTP response, then a normal IOException is thrown instead.如果代理有问题并且没有返回正确的 HTTP 响应,则改为抛出正常的 IOException。

Try setting the "kexTimeout" (Timeout for complete connection establishment (non-negative, in milliseconds). Zero means no timeout.) to non-zero尝试将“kexTimeout”(完全连接建立超时(非负数,以毫秒为单位)。零表示没有超时。)为非零

connection.connect(null, timeout, timeout);

Also, in your catch print the full stacktrace to verify where the timeout occurs此外,在您的捕获中打印完整的堆栈跟踪以验证超时发生的位置

catch (Exception t) {
    //time3
    t.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM