mirror of
https://github.com/hierynomus/sshj.git
synced 2025-12-06 15:20:54 +03:00
Updated KeepAlive and RemotePF examples (#791)
- Set KeepAlive interval before connecting
This commit is contained in:
@@ -24,7 +24,7 @@
|
|||||||
<groupId>com.hierynomus</groupId>
|
<groupId>com.hierynomus</groupId>
|
||||||
<artifactId>sshj-examples</artifactId>
|
<artifactId>sshj-examples</artifactId>
|
||||||
<packaging>jar</packaging>
|
<packaging>jar</packaging>
|
||||||
<version>0.19.1</version>
|
<version>0.33.0</version>
|
||||||
|
|
||||||
<name>sshj-examples</name>
|
<name>sshj-examples</name>
|
||||||
<description>Examples for SSHv2 library for Java</description>
|
<description>Examples for SSHv2 library for Java</description>
|
||||||
@@ -55,7 +55,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>com.hierynomus</groupId>
|
<groupId>com.hierynomus</groupId>
|
||||||
<artifactId>sshj</artifactId>
|
<artifactId>sshj</artifactId>
|
||||||
<version>0.31.0</version>
|
<version>0.33.0</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -19,8 +19,9 @@ public class KeepAlive {
|
|||||||
final SSHClient ssh = new SSHClient(defaultConfig);
|
final SSHClient ssh = new SSHClient(defaultConfig);
|
||||||
try {
|
try {
|
||||||
ssh.addHostKeyVerifier(new PromiscuousVerifier());
|
ssh.addHostKeyVerifier(new PromiscuousVerifier());
|
||||||
|
// Set interval to enable keep-alive before connecting
|
||||||
|
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5);
|
||||||
ssh.connect(args[0]);
|
ssh.connect(args[0]);
|
||||||
ssh.getConnection().getKeepAlive().setKeepAliveInterval(5); //every 60sec
|
|
||||||
ssh.authPassword(args[1], args[2]);
|
ssh.authPassword(args[1], args[2]);
|
||||||
Session session = ssh.startSession();
|
Session session = ssh.startSession();
|
||||||
session.allocateDefaultPTY();
|
session.allocateDefaultPTY();
|
||||||
|
|||||||
@@ -19,6 +19,7 @@ public class RemotePF {
|
|||||||
client.loadKnownHosts();
|
client.loadKnownHosts();
|
||||||
|
|
||||||
client.connect("localhost");
|
client.connect("localhost");
|
||||||
|
client.getConnection().getKeepAlive().setKeepAliveInterval(5);
|
||||||
try {
|
try {
|
||||||
|
|
||||||
client.authPublickey(System.getProperty("user.name"));
|
client.authPublickey(System.getProperty("user.name"));
|
||||||
@@ -33,8 +34,6 @@ public class RemotePF {
|
|||||||
// what we do with incoming connections that are forwarded to us
|
// what we do with incoming connections that are forwarded to us
|
||||||
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));
|
new SocketForwardingConnectListener(new InetSocketAddress("google.com", 80)));
|
||||||
|
|
||||||
client.getTransport().setHeartbeatInterval(30);
|
|
||||||
|
|
||||||
// Something to hang on to so that the forwarding stays
|
// Something to hang on to so that the forwarding stays
|
||||||
client.getTransport().join();
|
client.getTransport().join();
|
||||||
|
|
||||||
|
|||||||
@@ -35,14 +35,29 @@ public abstract class KeepAlive extends Thread {
|
|||||||
setDaemon(true);
|
setDaemon(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* KeepAlive enabled based on KeepAlive interval
|
||||||
|
*
|
||||||
|
* @return Enabled when KeepInterval is greater than 0
|
||||||
|
*/
|
||||||
public boolean isEnabled() {
|
public boolean isEnabled() {
|
||||||
return keepAliveInterval > 0;
|
return keepAliveInterval > 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get KeepAlive interval in seconds
|
||||||
|
*
|
||||||
|
* @return KeepAlive interval in seconds defaults to 0
|
||||||
|
*/
|
||||||
public synchronized int getKeepAliveInterval() {
|
public synchronized int getKeepAliveInterval() {
|
||||||
return keepAliveInterval;
|
return keepAliveInterval;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Set KeepAlive interval in seconds
|
||||||
|
*
|
||||||
|
* @param keepAliveInterval KeepAlive interval in seconds
|
||||||
|
*/
|
||||||
public synchronized void setKeepAliveInterval(int keepAliveInterval) {
|
public synchronized void setKeepAliveInterval(int keepAliveInterval) {
|
||||||
this.keepAliveInterval = keepAliveInterval;
|
this.keepAliveInterval = keepAliveInterval;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user