我想使用 java 实现期望的“交互”命令。在 expect 中,可以打开 ssh session ,进行身份验证,然后使用“交互”命令将控制权交还给用户。这可能用java吗?我尝试过使用 expectJ、expect4J 和 expectForJava,但是文档很少,而且几乎没有关于如何执行此操作的示例。 TIA。
更新:“交互”命令引用,请查看:http://wiki.tcl.tk/3914
"Interact is an Expect command which gives control of the current process to the user, so that keystrokes are sent to the current process, and the stdout and stderr of the current process are returned."
最佳答案
如果有人感兴趣,我已经在 ExpectIt 中添加了基本的交互式循环支持。 ,我自己开源的Expect for Java实现(自吹自擂),从0.8版本开始。
这是一个 example在 Java 8 中与系统输入流交互:
try (final Expect expect = new ExpectBuilder()
.withInputs(System.in)
.build()) {
expect.interact()
.when(contains("abc")).then(r -> System.out.println("A"))
.when(contains("xyz")).then(r -> System.err.println("B"))
.until(contains("exit"));
System.out.println("DONE!");
}
System.in.close();
关于java - 如何使用java实现expect "interact"命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15143619/