1

I write a tool with java to run a command at the remote machine. I use Java JSch lib to connect remote Windows10 by OpenSSH. When I send the command to Windows10, I got the result with some messy code. Like this

[?25h[?25labc@DESKTOP-ABCD C:\Users\abc>[?25h[?25ldir
[?25h[?25l Volume in drive C is Windows[18X[18C
 Volume Serial Number is 667E-DFE0[13X[13C
[47X[47C
 Directory of C:\Users\abc[20X[20C
[47X[47C
2020/11/25  14:11    <DIR>          .[10X[10C
2020/11/25  14:11    <DIR>          ..[9X[9C
2017/03/19  06:03    <DIR>          Desktop    
2020/11/25  14:11    <DIR>          Documents  
2017/03/19  06:03    <DIR>          Downloads  
2017/03/19  06:03    <DIR>          Favorites  
2017/03/19  06:03    <DIR>          Links      
2017/03/19  06:03    <DIR>          Music      
2020/01/26  15:34    <DIR>          OneDrive   
2017/03/19  06:03    <DIR>          Pictures   
2017/08/25  14:53    <DIR>          Roaming    
2017/03/19  06:03    <DIR>          Saved Games
2017/03/19  06:03    <DIR>          Videos     
]0;c:\windows\system32\cmd.exe - dir[?25h[?25l
               0 File(s)              0 bytes[35X[35C
[?25h[?25l
              13 Dir(s)  341,689,794,560 bytes free[29X[29C
[?25h[?25l
[80X[80C
]0;c:\windows\system32\cmd.exe[?25h[?25l
abc@DESKTOP-ABCD C:\Users\abc>[45X[45C[24;36H[?25h

There isn't an Active Code Page error. I set the Active Code Page:65001.(UTF-8) I am very confused. What are the garbled characters at the beginning and end of certain lines? How can I get rid of them?

Devin
  • 21
  • 2
  • 3
    Those are [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code), used by the program in question to try to produce colorful output. – Andreas Dec 18 '20 at 08:59
  • @Andreas I use string COLORS_REGEXP_STRING = "\\x1b\\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]" to matcher and replace the colorful output already. But they always there. – Devin Dec 18 '20 at 09:12
  • @Martin Prikryl Very Thanks. I fix the problem with setPty(false). – Devin Dec 18 '20 at 10:36
  • For more set [this discussion](https://stackoverflow.com/questions/52701215/getting-unwanted-characters-when-reading-command-output-from-ssh-server-using-js?answertab=votes#tab-top) . – Devin Dec 21 '20 at 01:47

0 Answers0