Search code examples
javaandroidshellsshjsch

Android N with JSCH shell channel issue


I'm using JSCH library for connecting via SSH in my Android app. For some reasons when I'm using Android N developer preview I received following stacktrace:

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)

This error I receive after connection process when I try to input some data to the ssh shell session.

In Android 6.0 and below everything is OK.

Could you help me with this strange behavior, please?

Thank you in advance!


Solution

  • I have faced the same issue. I was using PrintStream to input commands before but it doesn't work on Android 7.0. I have no idea why.

    The solution is to use PipedInputStream instead. Here is the example:

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