4

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

Nick Russler
  • 4,608
  • 6
  • 51
  • 88

3 Answers3

4

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.

ShaMan-H_Fel
  • 2,139
  • 17
  • 24
  • 1
    +1. JFI: There exist different terminal types (ANSI, VT100 are the most popular but there exist others as well), so in generic case more general approach would be probably needed. – Eugene Mayevski 'Callback Aug 26 '11 at 14:26
  • I agree, in the general case some way to detect the terminal type should be in place. The terminal type is generally stored in the TERM environment variable. See http://tldp.org/HOWTO/Keyboard-and-Console-HOWTO-11.html for details. – ShaMan-H_Fel Aug 26 '11 at 14:29
1

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

Tyler
  • 185
  • 5
1

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.)

  1. 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.

  2. 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.

Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210