有趣的Shell输出:[01; 32mtestfile.txt [00m而不是testfile.txt [英] Funny Shell Output: [01;32mtestfile.txt[00m instead of testfile.txt

查看:149
本文介绍了有趣的Shell输出:[01; 32mtestfile.txt [00m而不是testfile.txt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题我在使用 ChannelShell ,我想知道如何获得您在PuTTY会话中看到的常规输出。



通过有趣我的意思是我应该看到这个:

  testfile.txt 

我看到了这个:

  [01; 32mtestfile.txt [00m 

这类似于这个问题除了答案不满足我的需要。答案是在ChannelShell上调用 setPty(false),完全删除伪终端,但我需要实时获取ChannelShell的输出。这是我正在做的一个例子:

  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); //执行下一个命令取决于这里的输出,这就是为什么我需要它没有有趣的字符。
}
});
PipedInputStream in = new PipedInputStream();
channelInput = new PipedOutputStream(in);
channel.setInputStream(in);
channel.connect();

while(!channel.isClosed()&& channelWaitRetries ++< MAX_CHANNEL_WAIT_RETRIES){
//等待额外输出...排序超时协议。有点黑客......
睡觉(2500); //调用Thread.sleep。我只是不想在这里尝试/捕获。
System.out.println(频道尚未关闭。已重试+channelWaitRetries +of+ MAX_CHANNEL_WAIT_RETRIES);
}

sendNextCommand方法用于检查输出是否与需要显示下一个要执行的命令的内容。所以基本上,当我看到这样的东西: [user @ server~] $ 然后执行: ls 哪个应该返回: testfile.txt 但是而不是它返回这个: [01; 32mtestfile.txt [00m (注意:我不能复制并粘贴第一个字符,但它是一个包含字符代码27的框,我认为它是一个转义字符。)



<现在我通常只是逃避这种事情,但我宁愿把它做​​得恰到好处,而且似乎还有很多变种让人觉得奇怪。我在这里。我希望你可以提供帮助:)



注意:我通过我的IDE机器运行这个(eclipse rip-off),但我试过调试而变量输出实际上显示了有趣的字符。我也尝试在 JOptionPane.messageDialog 中显示它,只是为了确保它不仅仅是IDE而且它仍然有字符。谢谢!

解决方案

是的,这些是ANSI转义序列,在 ls ,以控制颜色。



ls 有一个 - color =从不选项应解决特定的 ls 问题。



这是否发生在所有问题上命令,或只是一些?



调用 ChannelShell.setPtyType(dumb)也可以提供帮助(未经测试)。您可能需要摆弄以找到禁用转义序列的终端类型。



您正在炮击的任何特殊原因 ls 而不是使用内置的Java方法来检查目录内容?


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!

解决方案

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?

这篇关于有趣的Shell输出:[01; 32mtestfile.txt [00m而不是testfile.txt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆