是什么原因导致ch.ethz.ssh2.Connection有一个挂起时间?

时间:2017-06-15 15:04:34

标签: java ssh ganymede

我目前正在使用ch.ethz.ssh2.Connection连接到java中的服务器。有时候它挂在一台服务器上(可能就像10-15秒)。我想知道导致这个挂起时间的原因以及如何避免它。

连接示例

    conn = new ch.ethz.ssh2.Connection(serverName);
    conn.connect();

    boolean isAuthenticated = conn.authenticateWithPassword(user, pass);
    logger.info("Connecting to " + server);

    if (isAuthenticated == false) {
        logger.info(server + " Please check credentials");              
    } 

    sess = conn.openSession();

   // I am connecting to over 200 servers and closing them. What would be the best practice to loop thru all these servers in the minimal time.

//some servers quickly connects, while some takes some time.
why does this happen?

2 个答案:

答案 0 :(得分:1)

主要问题是:是代码问题,网络问题还是服务器问题。

可以调试代码问题 - 遗憾的是ch.ethz.ssh2.Connection没有任何记录可能来检测内部的内容。

可能你应该关于切换ssh库(或者用它来处理有问题的服务器的一些测试)。根据我的经验sshj非常有用。

如果是网络问题或服务器问题,您可以通过Wireshark检查发生了什么。如果发送网络数据包但响应延迟,则问题不是使用的客户端代码。

答案 1 :(得分:0)

我的psychic debugging powers告诉我服务器正在对连接的每个客户端的IP地址进行DNS查找。这些DNS查找要么花费很长时间才能完成,要么完全失败。 DNS查找将阻止身份验证过程,直到成功完成或不成功。

如果服务器为the OpenSSH server,则此行为由sshd config“UseDNS”选项控制。