简体   繁体   English

Java Ganymed Trlead Orion ssh会话超时

[英]Java Ganymed Trlead Orion ssh session timeout

I am using the SSH Ganymed library alias Trilead alias Orion. 我正在使用SSH Ganymed库别名Trilead别名Orion。

I am trying to understand the exact behaviour of the session as I would need to keep an ssh connection opened for a long time (maybe for ever) and to close it just when my jvm goes down or something like this. 我试图理解会话的确切行为,因为我需要保持ssh连接打开很长时间(也许永远),并在我的jvm关闭或类似的事情时关闭它。

So, my problem is this. 所以,我的问题是这个。 Assuming I do something like this: 假设我做了这样的事情:

Connection conn = new Connection(this.hostName, this.port);
conn.addConnectionMonitor(new ConnectionMonitor()
{            
  @Override
  public void connectionLost(Throwable reason)
  {
     System.out.println("Connection Lost "   reason.getMessage());
  }
});
conn.connect(null, 1000, 20000);
conn.authenticateWithPublicKey(this.user, keyfile, this.password);
Thread.sleep(30000); //sleep the Thread for 30 seconds
Session sess = conn.openSession();
sess.execCommand("ls");
conn.close();

And, in those 30 seconds when the thread is sleeping, I disconnect my network interface for emulating a network issue. 并且,在线程休眠的30秒内,我断开网络接口以模拟网络问题。

1)The disconnect event is not intercepted by the connectionMonitor and the Connection Lost message is not printed 2)When 1)connectionMonitor不拦截disconnect事件,并且不打印Connection Lost消息2)When

Session sess = conn.openSession();

is executed, the process blocks and nothing happens until i don't not connect the network interface again. 执行后,进程阻塞,没有任何反应,直到我再也没有连接网络接口。 This is because, looking at the Ganymed code, it seems that since the disconnection event is not detected the session is opened and there's a lock opening the session until it does succeed. 这是因为,查看Ganymed代码,似乎由于未检测到断开事件,会话被打开,并且会打开会话直到成功。

So my questions are: 1)Is this behaviour wanted or is this a bug? 所以我的问题是:1)这种行为是否需要或者这是一个错误? 2)Is there any way to set a timeout in Connection.openSession() method as well as the Connection.connect() method? 2)有没有办法在Connection.openSession()方法和Connection.connect()方法中设置超时?

Thanks in advance. 提前致谢。

I think the problem is that you cannot simulate ssh disconnection by unplugging the cable for 30 seconds. 我认为问题是你不能通过拔掉电缆30秒来模拟ssh断开连接。 Have a look at this: If you open an ssh connection on a terminal an unplug the cable the ssh client will block as well, but will automatically reconnect after the cable is plugged in again. 看看这个:如果你在终端上打开一个ssh连接,拔下电缆,ssh客户端也会阻塞,但是在再次插入电缆后会自动重新连接。 I assume your code will do the same. 我假设您的代码也会这样做。

You normally get disconnected if there is no data sent between client and server for a configured amount of time, ie a period of inactivity, regardless of whether the cable is plugged in or not. 如果客户端和服务器之间没有数据在配置的时间内发送,即无论是否处于非活动状态,无论是否插入电缆,您通常都会断开连接。 This period is most likely larger than 30 seconds by default. 默认情况下,此期间很可能大于30秒。

On the sshd server you can configure 在sshd服务器上,您可以配置

ClientAliveInterval ClientAliveInterval

TCPKeepAlive TCPKEEPALIVE

ClientAliveCountMax ClientAliveCountMax

These parameters will determine how long the server will wait before expecting some data from the client, and how often he will accept a simple keep-alive packet rather than real data. 这些参数将决定服务器在从客户端获得某些数据之前等待多长时间,以及他接受简单的保持活动数据包而不是实际数据的频率。 On the client you can configure 在客户端上,您可以配置

ServerAliveInterval ServerAliveInterval

If the ClientAliveInterval is smaller than the ServerAliveInterval - the server expects keep-alive packets more often than the client sends them - you will be disconnected after the ClientAliveInterval amount of inactivity. 如果ClientAliveInterval小于ServerAliveInterval - 服务器期望保持活动的数据包比客户端发送的更频繁 - 在ClientAliveInterval不活动量之后,您将断开连接。 With that you can test your code. 有了它,你可以测试你的代码。

These configurations are also the key to keeping your ssh connection open indefinitely. 这些配置也是保持ssh连接无限期打开的关键。 If your ServerAliveInterval is smaller than the ClientAliveInterval - the client will send keep-alive packets more often than the server expects them - you connection will be open indefinitely. 如果您的ServerAliveInterval小于ClientAliveInterval - 客户端将比服务器期望的更频繁地发送保持活动数据包 - 您的连接将无限期地打开。

Have alook here https://www.simplified.guide/ssh/disable-timeout 请点击这里https://www.simplified.guide/ssh/disable-timeout

I would think that a restart of the server or client machine would still lead to a disconnection. 我认为重启服务器或客户端机器仍然会导致断开连接。 In case of your client machine restrating you would need to run your whole code again anyway, in case of a server machine restart the connection monitor would kick in and could reconnect. 如果您的客户端计算机正在进行重新设置,则无论如何都需要再次运行整个代码,如果服务器重新启动,连接监视器将启动并重新连接。

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

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