Search code examples
javasshtimeoutsshj

How to keep SSH connections alive using SSHJ?


I am developing a library that uses SSHJ for SFTP transfer. Since requests are frequent, I have wondered whether I could just keep the connection open.

Obviously, this will achieve nothing if the server frequently times out the connection. Since I have no control of the server, I have to keep the connection alive: With a regular SSH client, I could specify a ServerAliveInterval and have the client do it for me.

I'd like to do the same with SSHJ, but I do not know what message to send.

The SSH manual just states that ServerAliveInterval

Sets a timeout interval in seconds after which if no data has been received from the server, ssh(1) will send a message through the encrypted channel to request a response from the server.

So I'm wondering: What message is sent? How could I reproduce this message through SSHJ?


Solution

  • Sorry this isn't documented, but you can set a heartbeat:

    https://github.com/hierynomus/sshj/blob/master/examples/src/main/java/net/schmizz/sshj/examples/RemotePF.java#L51