英文:
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 "";
}
这可能是由于编码引起的吗?如果可能的话,请提出对上述方法的修改建议。非常感谢。
<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() > 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][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("(\\x9B|\\x1B\\[)[0-?]*[ -\\/]*[@-~]", "");
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论