hierynomus / hierynomus/sshj

Can't make read timeout work on inputstream

Open
#577 2 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
2.7k
Forks
620
Avg merge
3d 23h
Merged PRs (30d)
11

Description

Hi

I'm trying to write a program that access an equipment with SSH, listen in and when a certain amount of time has passed (10 seconds) without any characters coming in that would timeout from the read stream. I have looked at examples around the web and wrote this following code, but it does not work. The call to the read does not timeout at 10 seconds. Am I doing something wrong? Is there a bug in the library?

Thanks for your help...

Here is the code for the client:

`import java.io.*;

import net.schmizz.sshj.SSHClient;
import net.schmizz.sshj.connection.channel.direct.Session;
import net.schmizz.sshj.connection.channel.direct.SessionChannel;
import java.io.IOException;
import java.net.SocketTimeoutException;
import net.schmizz.sshj.transport.verification.PromiscuousVerifier;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Test implements Runnable {
private static Logger log = LogManager.getLogger(Test.class);

private SSHClient sshClient=null;
private SessionChannel shell = null;
private InputStream inputStream=null;

public static void main(String... args) throws IOException {
Runnable runnable = new Test();
Thread thread=new Thread(runnable);
thread.start();

}

public void run() {
log.debug("Creating SSHClient");
sshClient = new SSHClient();

sshClient.addHostKeyVerifier(new PromiscuousVerifier());

sshClient.setTimeout(10000);
try {
sshClient.connect("localhost", 2224);

sshClient.authPassword("xxxxxxxx", "xxxxxxxx");

Session session = sshClient.startSession();
shell= (SessionChannel) session.startShell();

inputStream = shell.getInputStream();

try (BufferedInputStream in = new BufferedInputStream(inputStream)) {

boolean stayIn = true;
byte[] buffer = new byte[1024];

log.debug("Timeout was set to {}",sshClient.getTimeout());
while (stayIn) {
int read = 0;

try {
if (log.isDebugEnabled()) {
log.debug("Reading/Waiting stream");
}
read = in.read(buffer);
if (read!=-1) {
log.debug("Read {} characters", read);
}
else {
log.debug("End of stream", read);
break;
}
} catch (SocketTimeoutException e) {
if (log.isDebugEnabled()) {
log.debug("SocketTimeoutException:, read={}", read);

}
}
}
}
} catch (Exception e) {
log.error("SSH Could not connect",e);
}

}
}`

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.