1

我正在尝试编写一个小应用程序,它可以自动使用外部应用程序,即 cisco any connect 移动客户端。它提供了一个命令行工具,您可以使用它来连接您的 VPN。

我想使用 apache commons-exec 库从我的 java 应用程序运行这个命令行工具,并能够读取他的输出以发送所需的信息。

我已经在网上搜索以找到“如何与外部应用程序通信”,但我发现的唯一帖子是这篇文章:Trouble provide multiple input to a Command using Apache Commons Exec and extracting output where it just says“嘿,我找到了解决方案”,但我不明白他是如何做到的。

当我开始这个过程时,我运行一个读取输入的函数,如下所示:

Thread T = new Thread() {
        public void run() {
            String line;
            try {
                line = processOutput.readLine();
                while (line != null) {
                    System.out.println(line);
                    if(line.contains("VPN-Password")){
                        sendMessage(processInput, "1");
                    }
                    if(line.contains("Please enter your username     and password")){
                        sendMessage(processInput, "username");
                    }
                    line = processOutput.readLine();
                }
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    };
    T.start();

函数发送消息只是运行一个线程写入进程输入流然后刷新它。

Thread T = new Thread() {
        public void run() {

            try {
                os.write((message+"\n").getBytes());
                os.flush();
                System.out.println("SENT : "+message);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }


        }

    };
    T.start();

如您所见,我检查输出以根据它向进程发送消息(基本上是为了回答问题)。但是,当涉及到“请输入...”时,我得到了这个异常

java.io.IOException: Read end dead

我的问题是,我找不到如何通过读取他的输出并根据它告诉我的内容向它发送消息来与流程“沟通”。

你能帮助我吗 ?

谢谢阅读。

4

0 回答 0