Why do I see scrabbled output when using JSch?

2019-06-13 18:31发布

问题:

I am trying to use JSch. I tried the example here Although I can connect the output is weird.
I get the following:

Last login: Thu Jan 31 19:44:25 2013 from 10.2.251.77

[1mcli:~ # [m  

And if I do e.g. an ls I get:

[0m[01;34m.InstallAnywhere[0m  [00m.bash_history                [00m.bash_profile[0m       
[01;34mbin[0m                             [00msles11-patched[0m  
[01;34m.kbd[0m              [00mindex.html[0m                      [00mtest.sql[0m  
[00m.viminfo[0m          [00;31mipvsadm-1.26-1.src.rpm[0m  
[m[1mcli:~ # [m  

These are the directory contents but why are they displayed like that?
I am running in this from Eclipse and this is what I see in Eclipse output. If I run this from Windows CMD it stucks

Update:
I noticed that if I connect to a different linux the output is fine!
Only if I connect to a specific linux installation I see these weird characters! Any idea what is causing this?

Update2:
Following the link of @PeterMmm I did printf "äöü" | xxd. Both the "bad" and good one give:
0000000: e4f6 fc
I also did locale.
In the "bad" case:

# locale  
LANG=POSIX  
LC_CTYPE=en_US.UTF-8  
LC_NUMERIC="POSIX"  
LC_TIME="POSIX"  
LC_COLLATE="POSIX"  
LC_MONETARY="POSIX"  
LC_MESSAGES="POSIX"  
LC_PAPER="POSIX"  
LC_NAME="POSIX"  
LC_ADDRESS="POSIX"  
LC_TELEPHONE="POSIX"  
LC_MEASUREMENT="POSIX"  
LC_IDENTIFICATION="POSIX"  
LC_ALL=  

In the good system:

LANG=POSIX  
LC_CTYPE=en_US.UTF-8  
LC_NUMERIC="POSIX"  
LC_TIME="POSIX"  
LC_COLLATE="POSIX"  
LC_MONETARY="POSIX"  
LC_MESSAGES="POSIX"   
LC_PAPER="POSIX"  
LC_NAME="POSIX"  
LC_ADDRESS="POSIX"   
LC_TELEPHONE="POSIX"   
LC_MEASUREMENT="POSIX"   
LC_IDENTIFICATION="POSIX"  
LC_ALL=  

Configuration seems to be the same. So what could be causing this?

回答1:

Please check Funny Shell Output: [01;32mtestfile.txt[00m instead of testfile.txt

((ChannelShell) channel).setPtyType("dumb");

Does the trick.



回答2:

They are escape sequences for the terminal emulation. I guess that there is no relation to the character encoding.

Update: If ChannelShell#setPty(false) is invoked, a pseudo-terminal will not be allocated and escape sequences will not be appeared.

Channel channel=session.openChannel("shell");
((ChannelShell)channel).setPty(false);                  // !!
...
channel.connect();