远程执行交互式CLI程序

时间:2011-12-15 08:29:10

标签: java exec command-execution

我编写了一个可以远程执行系统命令的小型客户端/服务器应用程序。当您只需要阅读其输出时,它可以与非交互式程序一起使用。但是,是否有一种可靠的方法将用户输入发送到它需要的进程启动?我如何确定进程是否提示输入?

1 个答案:

答案 0 :(得分:1)

看看' expectj'。这是http://en.wikipedia.org/wiki/Expect

的端口

http://expectj.sourceforge.net/

示例:

// Create a new ExpectJ object with a timeout of 5s
ExpectJ expectinator = new ExpectJ(5);

// Fork the process
Spawn shell = expectinator.spawn("/bin/sh");

// Talk to it
shell.send("echo Chunder\n");
shell.expect("Chunder");
shell.send("exit\n");
shell.expectClose();
相关问题