Android N 与 JSCH shell 频道问题

Android N with JSCH shell channel issue

我在 Android 应用程序中使用 JSCH 库通过 SSH 进行连接。由于某些原因,当我使用 Android N 开发者预览版时,我收到了以下堆栈跟踪信息:

W/System.err: com.jcraft.jsch.JSchException: SSH_MSG_DISCONNECT: 2 Packet corrupt 
W/System.err:     at com.jcraft.jsch.Session.read(Session.java:987)
W/System.err:     at com.jcraft.jsch.Session.run(Session.java:1381) 
W/System.err:     at java.lang.Thread.run(Thread.java:761)

当我尝试向 ssh shell 会话输入一些数据时,在连接过程后收到此错误。

在 Android 6.0 及以下版本中一切正常。

你能帮我解决这个奇怪的问题吗?

提前致谢!

我遇到过同样的问题。我之前使用 PrintStream 输入命令,但它在 Android 7.0 上不起作用。我不知道为什么。

解决方案是改为使用 PipedInputStream。 这是示例:

ChannelShell shellChannel = (ChannelShell) session.openChannel("shell");
shellChannel.setOutputStream(outputStream, true);
InputStream in = new PipedInputStream();
PipedOutputStream pin = new PipedOutputStream((PipedInputStream) in);
shellChannel.setInputStream(in);
shellChannel.connect();
pin.write("ls \r").getBytes());

我在使用 Android 7.1 和 jsch-0.1.54 时也遇到过类似问题。我的主机 OpenSSH /var/log/auth.log 显示 "Disconnecting: Packet corrupt" 和 "Bad packet length ..." 错误。

我在上面做了一些修改实现了 reaven 的解决方案:

初始化:

private Channel channel=null;
private InputStream in = null;
private PipedOutputStream pin =null;

异步连接逻辑:

channel= session.openChannel("shell");
in = new PipedInputStream();
pin = new PipedOutputStream((PipedInputStream) in);
channel.setInputStream(in);
channel.connect(3000);

异步命令生成和添加到输出流:

cmd=cmd+"\r\n";
pin.write(cmd.getBytes());
pin.flush();

一切似乎又恢复正常了。