1

我正在对一些网络设备进行一些维护,并且一直在使用 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("#"));

但我知道这是不对的,而且它不起作用。对实现对某个提示的反应和对其他提示的另一种反应的一些帮助将不胜感激。谢谢!

4

1 回答 1

1

您可以尝试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("#"));
}

希望能帮助到你。

于 2015-12-29T09:24:02.827 回答