我正在对一些网络设备进行一些维护,并且我一直在使用 Expectit 来浏览菜单。然而,只有当设备提供我期望的提示时,我才算成功。例如,当我登录时,某些设备已经处于启用模式,但有些设备则不然。
我想做相当于:
Expect expect = new ExpectBuilder()
.withOutput(channel.getOutputStream())
.withInputs(channel.getInputStream(), channel.getExtInputStream())
.withEchoOutput(wholeBuffer)
.withEchoInput(wholeBuffer)
.withExceptionOnFailure()
.build();
channel.connect();
if (expect.expect(contains(">")) {
expect.sendLine("enable");
expect.expect("assword:");
expect.sendLine(password);
}
expect.expect(contains("#"));
但我知道这是不对的,也是行不通的。对于实现对某个提示的 react 和对其他提示的另一种 react 的一些帮助将不胜感激。谢谢!
最佳答案
您可以尝试ExpectIt#interact但在0.8.0版本中它似乎被破坏了,所以尝试一下最新版本0.8.1。
如果没有 interact
,您可以使用 anyOf
匹配器,并根据各个结果的条件建立逻辑。这基本上就是interact
的工作原理。这是一个例子:
MultiResult multiResult = expect.expect(anyOf(contains(">"), contains("#")));
if (multiResult.getResults().get(0).isSuccessful()) {
expect.sendLine("enable");
expect.expect(contains("assword:"));
expect.sendLine(password);
} else if (multiResult.getResults().get(1).isSuccessful()) {
expect.expect(contains("#"));
}
希望有帮助。
关于java - 使用 Java 和 Expectit 交互进入启用模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34497508/