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?
Sorry this isn't documented, but you can set a heartbeat:
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
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.