4

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

2 Answers 2

8

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:

Sign up to request clarification or add additional context in comments.

Comments

0

After creating the channel, you can set the pseudo terminal type as "dumb"

ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setPtyType("dumb");

1 Comment

That about the same as setPty(false) and less straightforward.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.