5

I am using the SSH Ganymed library alias Trilead alias 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.

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.

1)The disconnect event is not intercepted by the connectionMonitor and the Connection Lost message is not printed 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.

So my questions are: 1)Is this behaviour wanted or is this a bug? 2)Is there any way to set a timeout in Connection.openSession() method as well as the Connection.connect() method?

Thanks in advance.

1 Answers1

3

I think the problem is that you cannot simulate ssh disconnection by unplugging the cable for 30 seconds. 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. 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, i.e. 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.

On the sshd server you can configure

ClientAliveInterval

TCPKeepAlive

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

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. With that you can test your code.

These configurations are also the key to keeping your ssh connection open indefinitely. 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.

Have alook here 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.

Community
  • 1
  • 1
MarcFasel
  • 1,080
  • 10
  • 19