逃逸码/随机字符在JSch SSH响应中。

huangapple go评论71阅读模式
英文:

Escape Codes/Random characters in JSch ssh response

问题

以下是翻译好的部分:

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 "";
}

通过以上方法打印响应(已删除部分文本) 逃逸码/随机字符在JSch SSH响应中。

如果在后续方法中使用或打印到文件中,将显示不同的随机字符。
逃逸码/随机字符在JSch SSH响应中。

这可能是由于编码引起的吗?如果可能的话,请提出对上述方法的修改建议。非常感谢。


<details>
<summary>英文:</summary>

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() &gt; 0) {
				int i = in.read(tmp, 0, 1024);
				if (i &lt; 0)
					break;
				return new String(tmp, 0, i);
			}
			if (channel.isClosed()) {
				break;
			}
		}
		return &quot;&quot;;
	}

Printing the response using above method (erased some texts) [![Screenshot of eclipse console][1]][1]

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

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

  [1]: https://i.stack.imgur.com/7Fwla.png
  [2]: https://i.stack.imgur.com/B841i.png

</details>


# 答案1
**得分**: 0

我正在使用执行通道,同时还有提示。添加了一个正则表达式来替换转义代码,现在内容更清晰可读。

```java
text.replaceAll("(\\x9B|\\x1B\\[)[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(&quot;(\\x9B|\\x1B\\[)[0-?]*[ -\\/]*[@-~]&quot;, &quot;&quot;);

huangapple
  • 本文由 发表于 2020年9月25日 15:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64059857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定