I'm trying to use the expectit library with sshj like so:
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());
}
}
The command I'm executing is "sleep 5; rm -i test.txt", I get the following output:
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)
One would think that everything should work fine as both the LoggingAppendableAdapter as well as the internal logging report the string "rm: remove regular empty file ‘test’?".
Any suggestions on what I might doing wrong?
The issue is that rm -i test.txt
writes its question to stderr
instead of stdout
, but expectit by default only checks the first input stream (which in my case was the stdout stream).
By using
expect.expectIn(1, contains(sshExpectation.getExpectation()));
expectit then expects the string to be on the second given input stream, which in my case is stderr
.