How to remove colors, etc. From ssh output

I am using jsch to output ssh from the local ssh server.

When I show the output in a text box, I get all this strange line in the output, for example:

] 0; ~ / rails_sites / rex_raid

[32mRob @shinchanii [33m ~ / rails_sites / rex_raid [0m

I assume that [33m and [0m mark the beginning of a new color or something else] 0; ~ marks a new line

How can I get rid of these errors when parsing the output for these lines?

Here is an example (not from me) what my output looks like:

http://www.google.de/codesearch#048v6jEeHAU/typescript&q=%5D0;~&l=1

+4
source share
3 answers

These are actually control sequences of the VT100 terminal. You can find their list (not sure if the list is complete) at http://www.termsys.demon.co.uk/vtansi.htm .

You can use the String 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 regular expressions that match all valid VT100 escape sequences. However, when creating a regular expression, do not forget that there is a non-printable ESC char in front of the square bracket (i.e., \ u001B in Unicode).

+4
source

These are ANSI escape sequences . As you might have guessed, they should be implemented by a terminal showing that they change the color or one of some font attributes. (They start with the escape character (ASCII 27), but this most likely does not appear in your text box.)

  • The right way to do this is to make your shell not print these codes if there is no (or dumb) terminal. But since they are often hardcoded in scripts (at least on my account here, invitation colors are hardcoded in .bashrc ), it can be tricky.

  • You can analyze these codes to disable them, or even interpret them (to make your text field bright). I once started implementing the last part, but I think that existing implementations may exist.

+1
source

I also use JSch and am experiencing the same issue.

For reference in JSch, Channel.setPtyType ("ansi"), before connecting, you can remove the ansi colors so that the output is acceptable on Windows.

Not sure if this option is compatible for all remote Linux / Unix servers.

+1
source

Source: https://habr.com/ru/post/1369166/


All Articles