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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…