【问题标题】:Expectit doesn't find my expected stringExpectit 没有找到我期望的字符串
【发布时间】:2018-01-02 02:26:30
【问题描述】:

我正在尝试将 expectit 库与 sshj 一起使用,如下所示:

final Session session = getSharedSession();
final Session.Command sessionCommand = session.exec(command);
try (Expect expect = new ExpectBuilder()
       .withOutput(sessionCommand.getOutputStream())
       .withInputs(sessionCommand.getInputStream(), sessionCommand.getErrorStream())
       .withInputFilters(removeColors(), removeNonPrintable())
       .withEchoInput(LoggingAppendableAdapter.getInstance())
       .withEchoOutput(LoggingAppendableAdapter.getInstance())
       .withExceptionOnFailure()
       .build()) {
    for (SshExpectations sshExpectation : sequence) {
        expect.expect(contains(sshExpectation.getExpectation()));
        expect.sendLine(sshExpectation.getReaction());
    }
}

我正在执行的命令是“sleep 5; rm -i test.txt”,我得到以下输出:

Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect start
FINE: Starting expect thread: input=< ChannelInputStream for Channel #1 >, charset=UTF-8, echoInput=LoggingAppendableAdapter@7a1099d7, filter=net.sf.expectit.filter.Filters$3@3afe1f22, bufferSize=1024
Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect start
FINE: Starting expect thread: input=< ChannelInputStream for Channel #1 >, charset=UTF-8, echoInput=LoggingAppendableAdapter@7a1099d7, filter=net.sf.expectit.filter.Filters$3@3afe1f22, bufferSize=1024
Jul 26, 2017 6:07:14 PM net.sf.expectit.ExpectImpl expectIn
FINE: Expect matcher 'contains('remove regular empty')' with timeout 30000 (ms) in input #0
Jul 26, 2017 6:07:14 PM net.sf.expectit.SingleInputExpect expect
FINE: Initial matcher contains('remove regular empty') result: SimpleResult{succeeded=false, before='null', group='null', input='', canStopMatching=false}
2017-07-26 18:07:19,574 INFO [expect-pool-26-thread-2] LoggingAppendableAdapter rm: remove regular empty file ‘test’? 
Jul 26, 2017 6:07:19 PM net.sf.expectit.InputStreamCopier call
FINE: Received from < ChannelInputStream for Channel #1 >: rm: remove regular empty file ‘test’? 
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect expect
FINE: Selector returns 0 key
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect stop
FINE: Releasing resources for input: < ChannelInputStream for Channel #1 >
Jul 26, 2017 6:07:44 PM net.sf.expectit.SingleInputExpect stop
FINE: Releasing resources for input: < ChannelInputStream for Channel #1 >
2017-07-26 18:07:46,800 ERROR Could not execute command
net.sf.expectit.ExpectIOException: Expect operation fails (timeout: 30000 ms) for matcher: contains('remove regular empty')
    at net.sf.expectit.ExpectImpl.expectIn(ExpectImpl.java:106)
    at net.sf.expectit.AbstractExpectImpl.expectIn(AbstractExpectImpl.java:57)
    at net.sf.expectit.AbstractExpectImpl.expect(AbstractExpectImpl.java:61)

人们会认为一切都应该正常工作,因为 LoggingAppendableAdapter 以及内部日志记录报告字符串“rm: remove regular empty file ‘test’?”。

关于我可能做错的任何建议?

【问题讨论】:

  • 我尝试使用 shell 而不是命令,它可以工作 - 但是我宁愿继续使用命令,因为我不必担心 shell 干扰......

标签: java sshj expectit


【解决方案1】:

问题是rm -i test.txt将其问题写入stderr而不是stdout,但默认情况下,expectit只检查第一个输入流(在我的例子中是stdout流)。

通过使用

expect.expectIn(1, contains(sshExpectation.getExpectation()));

然后期望字符串位于第二个给定的输入流上,在我的例子中是stderr

【讨论】:

    猜你喜欢
    • 2014-01-30
    • 1970-01-01
    • 2018-04-25
    • 2022-01-04
    • 1970-01-01
    • 2018-10-10
    • 1970-01-01
    • 2019-08-03
    • 1970-01-01
    相关资源
    最近更新 更多