0

I am using JSch to ssh to a Linux server and run a custom utility and checking response in java code. I see some random characters in response. The same looks fine while checking in Putty.

private String logResponse(Channel channel, InputStream in) throws IOException {
    byte[] tmp = new byte[1024];
    while (true) {
        while (in.available() > 0) {
            int i = in.read(tmp, 0, 1024);
            if (i < 0)
                break;
            return new String(tmp, 0, i);
        }
        if (channel.isClosed()) {
            break;
        }
    }
    return "";
}

Printing the response using above method (erased some texts) Screenshot of eclipse console

If it is used in subsequent methods or printed in files, it will display different random characters. enter image description here

Could it be due to encoding? Kindly suggest if the above method can be tweaked. Many thanks.

Nagaraja JB
  • 729
  • 8
  • 18

1 Answers1

0

I am using exec channel and there are prompts as well. Added a regex to replace escape codes and it is now readable.

text.replaceAll("(\\x9B|\\x1B\\[)[0-?]*[ -\\/]*[@-~]", "");
Nagaraja JB
  • 729
  • 8
  • 18