Expectit: Call "isSuccessful()" method without clear the Expect buffer (afthe check if successful i would like to get the result)

212 views
Skip to first unread message

giusep...@gmail.com

unread,
Oct 6, 2014, 5:42:19 AM10/6/14
to java-e...@googlegroups.com
Hi all,
I started to use this library few days ago for sending ssh commands to a linux prompt (whit jsch).
It is great, but I've just a question probably due to my inexperience. How can I retrieve the output of an "expect(matcher)" after I called on it "isSuccessful()"?
It seems that "isSuccessful" clear the buffer so loose the results, there is a manner (without the timeout exception) to obtain the result after calling isSuccessful() method?

Example:
expect.sendLine(A command);
if(!expect.expect(regexp(readyConsoleRegexp)).isSuccessful()){ //Here seems that the buffer is cleared but I would like to get the result if the error has been not verified.
System.out.println("ERROR");
return;
}
String result = expect.expect(regexp("$")).getBefore();

Alexey Gavrilov

unread,
Oct 6, 2014, 6:39:31 AM10/6/14
to giusep...@gmail.com, java-e...@googlegroups.com
Hi!

If I get you right you want to access the buffer contents if the match was unsuccessful in order, for example, to report an error to the user.  

Now it works like if the match is successfull then the buffer is cleared and the buffer state is returned as the result of the match, accessible is via result.getBefore() and the other result methods.

But if the match was not successful the resulting object does not contain the buffer data. This is a current limitation which I plan to fix it in the next release (0.6.0) very soon (in a few days). As a workaround for now I could recommend to call expect.expect(anyString()).getBefore() to access the buffer contents right way. 

Hope this helps and thank you for the feedback,
Alexey


--
You received this message because you are subscribed to the Google Groups "Yet another Expect for Java" group.
To unsubscribe from this group and stop receiving emails from it, send an email to java-expecti...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

giusep...@gmail.com

unread,
Oct 6, 2014, 7:52:48 AM10/6/14
to java-e...@googlegroups.com, giusep...@gmail.com, alexey1....@gmail.com
Hi Alexey, thanks for your fast reply (and for the reply to my question on stackoverflow few days ago).

> If I get you right you want to access the buffer contents if the match was unsuccessful in order, for example, to report an error to the user.  
>

Yes this is one of the reasons but not the only.

In general I would like to retrieve the result of a SSH command in any case and not only when it fails.

For example:
expect.sendLine("./giveMeTheResult.sh"); // I execute a command
if (expect.expect(regex(promptReadyRegex)).isSuccessfull()){ // The expect regex blocks the execution till there's not the linux prompt [root@myAddress]# )
//Also if it's successful I would like to obtain the buffer because I would like to read the result string that is located before the String "[root@myAddress]#" .
}
..

What do you think about the possibility to add to your library also another method called isSuccessful(booelan cleanBuffer) that take a boolean parameter that tells to the Expectit if the buffer should be cleared or not? In this manner the default "isSuccessful()" works exactly as now and the new "isSuccessful(boolean)" give to the programmer the choice of maintain the buffer.
Obviously this is just a suggestion it's possible that I'm not taking in consideration other wrong implication that this new behaviour could produce and the difficulty to implement it (I have not seen how the library is implemented).
For now I'll use your workaroud ;) .
Thanks for all your help (and for Expectit library) ;)
Have a nice day.


--
Giuseppe Tino

giusep...@gmail.com

unread,
Oct 6, 2014, 8:15:05 AM10/6/14
to java-e...@googlegroups.com, giusep...@gmail.com, alexey1....@gmail.com
:S Sorry, I completely misunderstand the "isSuccessful()" behaviour, i supposed that it was called on the expect object and that cleared the buffer returning just a value true or false.
Reading better your workaround now I understood that isSuccessful is called on a Result object and that I can also call the getBefore on the same result object.
Sorry for the mistake. You can not consider my previous email.
Thanks a lot.
Reply all
Reply to author
Forward
0 new messages