【问题标题】:Java SSH server with JLine3 and JSch client带有 JLine3 和 JSch 客户端的 Java SSH 服务器
【发布时间】:2018-01-01 09:19:42
【问题描述】:

我正在尝试使用 apache sshd-core 将基于 Java 的 SSH 服务器从使用 jline2 迁移到 jline3,并使用 JSch 客户端连接和执行 shell 命令。使用 jline2,一切正常。

使用 jline3,它在 OSX 中通过 SSH 客户端执行命令时工作得很好。但是,我似乎无法让它与 JSch 一起使用。

在下面附加堆栈跟踪:

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

可以在github找到示例项目

提前致谢。

【问题讨论】:

    标签: jsch jline2


    【解决方案1】:

    示例项目已使用有效的 JLine3 代码进行了更新。需要使用 PipedInputStream 和 PipedOutputStream 来执行此操作。例如:

    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();
    

    感谢gnodet's response

    【讨论】:

      猜你喜欢
      • 2011-11-26
      • 1970-01-01
      • 1970-01-01
      • 2011-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多