我正在尝试使用sshj的expectit库,如下所示:
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'?”。
关于我可能做错的任何建议?
答案 0 :(得分:0)
问题在于rm -i test.txt
将其问题写入stderr
而不是stdout
,但是默认情况下只会检查第一个输入流(在我的例子中是stdout流)。
使用
expect.expectIn(1, contains(sshExpectation.getExpectation()));
expectit然后期望字符串在第二个给定的输入流上,在我的例子中是stderr
。