Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting unwanted characters when reading command output from SSH server using JSch

I am connecting to the remote server SSH server and trying to get get the list of files in a particular path I am able to get list of files in the path but they were in unreadable format could any one help on this

String host="xxxxx.yyyy.com";
String user="user";
String password="password";
String command1="dzdo su - lucy";
try{    
    java.util.Properties config = new java.util.Properties(); 
    config.put("StrictHostKeyChecking", "no");
    JSch jsch = new JSch();
    Session session=jsch.getSession(user, host, 22);
    session.setPassword(password);
    session.setConfig(config);
    session.connect();
    System.out.println("Connected");

    Channel channel=session.openChannel("shell");
    OutputStream ops = channel.getOutputStream();
    PrintStream ps = new PrintStream(ops, true);

    channel.connect();
    ps.println(command1);
    ps.println("ls -ltr");
    InputStream in=channel.getInputStream();
    byte[] tmp=new byte[1024];
    while(true){
        while(in.available()>0){
            int i=in.read(tmp, 0, 1024);
            if(i<0)break;
            System.out.print(new String(tmp, 0, i));
        }
        if(channel.isClosed()){
            System.out.println("exit-status: "+channel.getExitStatus());
            break;
        }
        try{Thread.sleep(1000);}catch(Exception ee){}
    }
    channel.disconnect();
    session.disconnect();
    System.out.println("DONE");

Here is the console output

drwxrws---. 2 gleaiid gleai 4096 Jan 21  2016 [0m[01;34mold[0m
-rwxrws---. 1 jhon gleai  100 Jul 20  2017 [30;43mNVISAP_814_Test.txt[0m
-rwxrws---. 1 jhon  gleai  134 Jul 20  2017 [30;43mUS_NISC14_4164556_Test.txt[0m
-rwxrws---. 1 jhon  gleai    0 Jul 20  2017 [30;43mNVISAP_R00814_Test.trg[0m
like image 801
kushma gonna Avatar asked Jan 27 '23 08:01

kushma gonna


1 Answers

These are ANSI escape codes that are normally interpreted by a terminal client to pretty [color] print the output.

If the server is correctly configured, you get these only, when you use an interactive terminal. In other words, if you requested a pseudo terminal for the session (what you should not, if you are automating the session).

The JSch automatically requests the pseudo terminal, if you used a "shell" channel, as that is supposed to be used for implementing an interactive terminal.

  • If you automate an execution of remote commands, you better use an "exec" channel, as shown in JSch Exec.java example.

  • Alternatively, you can prevent JSch from requesting the pseudo terminal by calling setPty. But I do not recommend using "shell" channel. Though if you need to, for whatever reason, you should call setPty(false) in any case, as that will prevent many other similar troubles. I actually already recommended that to you in your previous question.


Note to others: While I see why OP uses ls command, in general, one should use use SFTP API to retrieve a directory listing, instead of executing ls and parsing its output. Parsing ls output is a pretty unreliable approach.


Related questions:

  • Removing shell stuff (like prompts) from command output in JSch
  • Is there a simple way to get rid of junk values that come when you SSH using Python's Paramiko library and fetch output from CLI of a remote machine?
like image 59
Martin Prikryl Avatar answered Jan 30 '23 03:01

Martin Prikryl