Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

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

Question: I'm getting "funny" character output from commands I send when using a ChannelShell and I'm wondering how to get just the regular output you see in a PuTTY session.

By "funny" I mean I should see this:

testfile.txt

And I'm seeing this:

[01;32mtestfile.txt[00m

This is similar to the problem in this question except the answer does not satisfy my need. The answer there is to call setPty(false) on the ChannelShell which removes the "pseudo-terminal" entirely, but I need to have the output from the ChannelShell in real time. Here's an example of what I'm doing:

ChannelShell channel = (ChannelShell) session.openChannel("shell");
channel.setOutputStream(new PrintStream(
    new ByteArrayOutputStream(), true, "UTF-8") {
        @Override
        public void write(byte[] b, int off, int len) {
            super.write(b, off, len);
            String output = new String(b, off, len);
            System.out.print(output);
            sendNextCommand(output); //Execution of the next command depends on the output here, this is why I need it to not have the funny characters.
        }
    });
PipedInputStream in = new PipedInputStream();
channelInput = new PipedOutputStream(in);
channel.setInputStream(in);
channel.connect();

while(!channel.isClosed() && channelWaitRetries++ < MAX_CHANNEL_WAIT_RETRIES) {
    //Wait for additional output... Sort of a timeout deal. Kind of a hack...
    sleep(2500); //Calls Thread.sleep. I just don't want the try/catch here.
    System.out.println("Channel not yet closed. Retried " + channelWaitRetries + " of " + MAX_CHANNEL_WAIT_RETRIES);
}

The sendNextCommand method is where the check is done to see if the output matches the what needs to be showing for the next command to execute. So basically, when I see something like this: [user@server ~]$ then execute this: ls which should return this: testfile.txt but instead it is returning this: [01;32mtestfile.txt[00m (NOTE: I can't copy and paste the first character, but it's a box with the char code of 27 which I think is an escape character).

Now I would normally just escape this kind of thing, but I'd rather get it done right, and it also seems like there are a number of variants to the weirdness. So here I am. I hope you can help :)

Note: I'm running this through my Windows machine through my IDE (an eclipse rip-off), but I've tried debugging and the variable output actually shows the "funny" characters. I've also tried showing it in a JOptionPane.messageDialog just to make sure it's not just the IDE and it still has the characters. Thanks!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, these are ANSI escape sequences, in the case of ls, to control the colour.

ls has a --color=never option which should resolve the specific ls problem.

Is this happening on all commands, or just some?

Calling ChannelShell.setPtyType("dumb") could also help (untested). You may need to fiddle around to find a terminal type that disables the escape sequences.

Any particular reason you are shelling ls rather than using built-in Java methods to check for directory contents?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...