0

I'm trying to migrate a Java based SSH server using apache sshd-core from using jline2 to jline3 and using a JSch client to connect and execute shell commands. With jline2, everything works just fine.

With jline3, it works just fine when executing commands over SSH client in OSX. However, I can't seem to get it working with JSch.

Attaching stacktrace below:

Exception in thread "Thread-4" org.jline.reader.EndOfFileException: org.jline.utils.ClosedException: InputStreamReader is closed.
    at org.jline.keymap.BindingReader.readCharacter(BindingReader.java:140)
    at org.jline.keymap.BindingReader.readBinding(BindingReader.java:109)
    at org.jline.keymap.BindingReader.readBinding(BindingReader.java:60)
    at org.jline.reader.impl.LineReaderImpl.readBinding(LineReaderImpl.java:709)
    at org.jline.reader.impl.LineReaderImpl.readLine(LineReaderImpl.java:515)
    at org.jline.reader.impl.LineReaderImpl.readLine(LineReaderImpl.java:385)
    at test.ssh.jline3.EchoSshSessionInstance.run(EchoSshSessionInstance.java:64)
    at java.lang.Thread.run(Thread.java:745)
Caused by: org.jline.utils.ClosedException: InputStreamReader is closed.
    at org.jline.utils.InputStreamReader.read(InputStreamReader.java:191)
    at org.jline.utils.NonBlockingReader.run(NonBlockingReader.java:273)
    ... 1 more

Sample project can be found at github

Thanks in advance.

anand1st
  • 1,086
  • 10
  • 16

1 Answers1

0

The sample project has been updated with working JLine3 code. Needed to use PipedInputStream and PipedOutputStream to do this. E.g:

JSch jsch = new JSch();
Session session = jsch.getSession("admin", "localhost", 8022);
session.setPassword("xxx");
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
ChannelShell channel = (ChannelShell) session.openChannel("shell");
PipedInputStream pis = new PipedInputStream();
PipedOutputStream pos = new PipedOutputStream();
channel.setInputStream(new PipedInputStream(pos));
channel.setOutputStream(new PipedOutputStream(pis));
channel.connect();
pos.write("exit\r".getBytes(StandardCharsets.UTF_8));
StringBuilder sb = new StringBuilder();
int i;
while ((i = pis.read()) != '\n') {
    sb.append((char) i);
}
assertEquals("exit\r", sb.toString());
channel.disconnect();
session.disconnect();

Thanks to gnodet's response

anand1st
  • 1,086
  • 10
  • 16