简体   繁体   中英

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. Some how we are getting exception while authenticating the user but after 60min. How can we decrease the timing to get the exception to 5min or so.

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. 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. Is this useful to resolve this issue. What might be the outcome if I decrease its value to some 30min or 5min.

Let me summarize your problem, please correct me if something wrong.

  1. You can not connect to SSH server.
  2. An exception raised after 60 mins.

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. 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).

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.

Try setting the "kexTimeout" (Timeout for complete connection establishment (non-negative, in milliseconds). Zero means no timeout.) to non-zero

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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