Reputation: 6921
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?
Upvotes: 5
Views: 5438
Reputation: 2459
Starting from version 0.11.0, you can use built-in KeepAliveProvider:
public class KeepAlive {
public static void main(String... args)
throws IOException, InterruptedException {
DefaultConfig defaultConfig = new DefaultConfig();
defaultConfig.setKeepAliveProvider(KeepAliveProvider.KEEP_ALIVE);
final SSHClient ssh = new SSHClient(defaultConfig);
try {
ssh.addHostKeyVerifier(new PromiscuousVerifier());
ssh.connect(args[0]);
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5); //every 60sec
ssh.authPassword(args[1], args[2]);
Session session = ssh.startSession();
session.allocateDefaultPTY();
new CountDownLatch(1).await();
try {
session.allocateDefaultPTY();
} finally {
session.close();
}
} finally {
ssh.disconnect();
}
}
}
To send heartbeats, you may use KeepAliveProvider.HEARTBEAT
.
Upvotes: 3