Java流程流 [英] Java Process streams

查看:89
本文介绍了Java流程流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在与某些终端应用程序交互时遇到问题(在我的情况下是openSSL).我有一个要发送的命令,然后此应用程序需要密码并回复给定的密码.我的代码不起作用.我的意思是我看不到任何输出.

I have a problem with interacting with some terminal application (in my situation it is openSSL). I have a command to send and then this application wants password and reply given password. My code doesn't work. I mean that I don't see any output from it.

为了进行测试,我还制作了一个简单的应用程序,该应用程序正在等待两个字符串键入并从我的Java代码运行它.

For testing I've made also simple application which is waiting for two strings to type and running it from my Java code works.

有什么建议吗?

ProcessBuilder pb = new ProcessBuilder("openssl.exe");
    Process process = pb.start();

    final InputStream is = process.getInputStream();
    new Thread(new Runnable() {
        public void run() {
            try {
                BufferedReader reader =
                    new BufferedReader(new InputStreamReader(is));
                String line;
                while ((line = reader.readLine()) != null) {
                    System.out.println(line);
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
    }).start();


    OutputStream out = process.getOutputStream();
    BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(out));

    writer.write("genrsa -des3 -out ca.key 512\n");
    writer.write("password\n");
    writer.write("password\n");
    writer.flush();

    writer.close();
    process.waitFor();

推荐答案

我认为,OpenSSL的行为不符合您的预期.我尝试了以下操作(在Linux上):

I think, OpenSSL does not behave as you expect. I tried the following (on Linux):

echo $'genrsa -des3 -out xxx.key 512\npassword\npassword\n' \
    | openssl 2>stderr.txt 1>stdout.txt

它提示我在终端上输入两个密码:

It prompts me for two password entries on the terminal:

Enter pass phrase for xxx.key:
Verifying - Enter pass phrase for xxx.key:

Stdout.txt收到:

Stdout.txt receives:

OpenSSL> OpenSSL> OpenSSL> OpenSSL> OpenSSL>

Stderr.txt收到:

Stderr.txt receives:

Generating RSA private key, 512 bit long modulus
..........++++++++++++
...........++++++++++++
e is 65537 (0x10001)
openssl:Error: 'password' is an invalid command.
openssl:Error: 'password' is an invalid command.

这意味着openslsls的大部分输出都将发送到stderr.它不会向stdout输出换行符,因此reader.readLine()将永远不会得到完整的行.这就是为什么您看不到任何输出的原因. (改为使用read().您可能还需要阅读stderr以避免阻塞.)

This means that most of openssls output goes to stderr. It does not output newlines to stdout, so that reader.readLine() will never get a complete line. That is why you do not see any output. (Use read() instead. You might also need to read stderr to avoid blocking.)

OpenSSL始终尝试从当前终端获取密码而不回显,而不是从stdin获取密码.在没有特殊选项的stdin上显示它们会导致解释为命令.有关详细信息和选项,请参见以下讨论:通过stdin安全地将密码传递给openssl

OpenSSL always tries to get the password from the current terminal without echoing, not from stdin. Presenting them on stdin without special options causes interpretation as commands. For details and options you have see this discussion: Securely passing password to openssl via stdin

这篇关于Java流程流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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