问题
i am using jsch to get ssh output from a local ssh server.
When i display the output in a textbox i get all these weird string in the output for example:
]0;~/rails_sites/rex_raid
[32mRob@shinchanii [33m~/rails_sites/rex_raid[0m
I guess [33m and [0m mark the begin of a new color or something and ]0;~ marks a newline
how do i get rid of these withput parsing the output for those strings ?
Here a example (not from me) how my output looks like:
http://www.google.de/codesearch#048v6jEeHAU/typescript&q=%5D0;~&l=1
回答1:
These are actually VT100 terminal control escape sequences. You can find a list of them (not sure if the list is complete) at http://www.termsys.demon.co.uk/vtansi.htm.
You can use the String's replaceAll method (http://download.oracle.com/javase/1.4.2/docs/api/java/lang/String.html#replaceAll%28java.lang.String,%20java.lang.String%29), and create a regular expressions which matches all valid VT100 escape sequences. However when creating the regexp do not forget that there is non printable ESC char (that is \u001B in Unicode) before the square bracket.
回答2:
These are ANSI escape sequences. As you guessed right, these are intended to be implemented by the terminal showing these to change color or one of some font attributes. (They start with an Escape character (ASCII 27), but this is likely not shown in your text box.)
The right way to do this would be to make your shell not print these codes if there is no (or a dumb) terminal. But since they are often hard-coded in scripts (at least on my account here the prompt-colors are hard-coded in
.bashrc
), this might not be easy.You can parse these codes, either to strip them off, or to even interpret them (to make your textbox colorful). I once started to implement the last part, but I think there might be existing implementations around.
回答3:
I'm also using JSch and experience the same problem.
For you reference, in JSch, Channel.setPtyType("ansi") before connect can remove the ansi colors so that the output is acceptable in Windows.
Not sure if this setting is compatible for all remote Linux/Unix servers
来源:https://stackoverflow.com/questions/7205837/how-to-remove-colors-etc-from-ssh-output