|
Hi I seem to have run into a timing snag. When I start the session, the server sends me back 4 lines, the last of which being "login:". Then I need to enter the login with a newline, then wait for "Password", send the password, then wait for the prompt (identified by a ">" at the end). Here's my code
I never get into the first if, but if I dump the contents of the virtual terminal after the first if
then I see the "login:" I'm looking for. When I immediately send the login, then wait for "Password" - same story. So it seems that terminal.Expect only works from the point in time where it has been called... and if the server is fast enough and has already sent the expected string, Expect will fail. As I will be running commands that take longer (and interactive operations where responses don't always come below my input but may update the existing lines - so using the Shell object is out of the question), I need to find a way to accommodate both. Is there a way to make Expect also consider what has already been received so I won't have to resort to the clumsy way of dumping the contents of the virtual terminal each time expect returns false? Regards Stephan |
|
Hello Stephan, VirtualTerminal runs in selfprocessing mode. It means that until you call the Process method or the Expect method no data are received. Therefore wrong timing hypothesis should not be the cause. The Expect method only checks the end of the response (not the whole response content). Please see the method documentation:
So I have a suspicion that the server sends "login: " (please note the last space) while you are expecting "login:" (without the last space). From the definition of the Expect method it should return false in this case. Please update your code to include the last space:
If this is not the cause, please let us know here. |
|
In the meantime, I realized that I misunderstood the meaning of Expect. It only works if the END! response precisely matches the string I'm looking for. What I need is a bit more generic - I don't always know the precise end of the response (it can be ambiguous), so I wrote the following wrapper method which mimics the behavior I have in other libs that I've used previously:
using VirtualTerminal.Process with a loop that checks the contents of the virtual terminal does the trick for me. |
|
Sorry for the delayd answer, I missed your post. This is a dark side of the programming automatic shell scripts. Please note that if the expected response is split into two lines, your
In my opinion, you should “almost always” know how the response exactly ends. In case there are more possible endings we can add the Expect(string[] responses, int maximumWaitTime) method. In case you really don’t know the ending of the response you have to keep in mind these scenarios:
Therefore you should always try to receive the whole server response to be able to determine where the response of the next command starts. Here is some code snipped of a more robust WaitForString method (at least from my point of view):
|