RegEx matching on WaitFor or Prompt - WeOnlyDo Discussion board

RegEx matching on WaitFor or Prompt (General questions)

by rsg98, Wednesday, August 03, 2005, 19:38 (7051 days ago)

Been upsetting me all day this...
[code]
Try
SshConnection.Blocking = True
SshConnection.Send(strCmd & vbCrLf)
SshConnection.WaitFor( fullprompt-> )

strBuffer = SshConnection.Receive
strBufferToUse = strBuffer

Debug.WriteLine( ***BUFFER_STARTS** )
Debug.Write(strBuffer)
Debug.WriteLine( ***BUFFER_ENDS**** )

Catch ex As Exception
Return FirewallErrors.FWERROR_IOERROR
End Try
[/code]

How does the receive buffer work? The device I'm connecting to returns similar to this on connection:

Remote management interface
fullprompt->

Then I send it a command using the function above (which is separate to the actual connect function...

get blah

And using the above code, it works - but, the .Receive buffer contains the Remote management interface... etc. - so I think the Waitfor trips on the /first/ prompt, rather than waiting until the get blah command has completed then returning the data from that.

If I add in Debug.Print(SshConnection.Receive) before the .Send (to try and clear the buffer) then I don't get any of the expected results - the .Receive from the .Send command is empty.

Next (and the problem which pushed me away from .Execute towards trying with .Send) is using a regex instead of the fullprompt->. Using

regex:-> $

...which is a syntax which works for .NET native regex, makes the whole thing behave very strangely.

For now, I'd just be happy understanding how the .Receive buffer is supposed to work (is this a timing issue, waitfor running against existing buffer info before .send completes?)- or alternatively, figuring out a regex that will work with .Execute.

Huge... complicated... probably too rambling to solve!

Thanks,

Richard

Re: RegEx matching on WaitFor or Prompt

by wodSupport, Wednesday, August 03, 2005, 20:29 (7051 days ago) @ rsg98

Richard,

you were right. Waitfor isn't that smart to wait for command to be executed. Rather, it waits for your fullprompt> and when it find it it returns.

Now, you have a problem if you don't consume first fullprompt> that was sent by the server immediately upon login. If you don't eat that one, you will get invalid responses all the time.

Can you try that?

Kreso

Re: RegEx matching on WaitFor or Prompt

by rsg98, Thursday, August 04, 2005, 12:42 (7050 days ago) @ wodSupport

If you don't eat that one, you will get invalid responses all the time.

Can you try that?

Thanks - that explains it. I added:

[code]
'Clear receive buffer
strBuffer = SshConnection.Receive
Debug.Write( *** Initial Data *** & vbCrLf & strBuffer)
[/code]

...to my connection method - and now it behaves itself. I'm also a lot happier because I've gone back to using .Execute, which blocks in the way I'm expecting it to. But, still having problems with the regex side of things. I've tried:

.Prompt = myprompt-> <- Works!
.Prompt = regex:myprompt-> <- Works!
.Prompt = regex:--- more --- |myprompt-> <- No matches!

The last one is frustrating - because without being able to do that, I can't use .Execute and I'll get back into the timing mess I was in before. The regex seems to be right according to:

http://www.dotnetcoders.com/web/Learning/Regex/RegexTester.aspx

What I need to do is be able to deal with commands that return paged results, e.g.

myprompt-> get mylist
item1
item2
item3
--- more ---

I then read this into a buffer and do something like

[code]
If strBuffer.Endswith( --- more --- ) Then
Do While strBuffer.Endswith( --- more --- )
strBuffer = strBuffer & SshConnection.Execute(vbCrLf)
Loop
End If
[/code]

So when there's no more --- more --- prompts, the loop should exit.

This means that if I set

[code]
.Prompt = --- more ---
[/code]

Then I have a method that works for commands which return paged results. Or I can set

[code]
.Prompt = myprompt->
[/code]

To have a method working for non-paged results.

But not, it seems, both! Any ideas?

Thanks,

Richard

Re: RegEx matching on WaitFor or Prompt

by rsg98, Thursday, August 04, 2005, 12:46 (7050 days ago) @ rsg98

Also, it occurs to me that my loop won't work, because using .Prompt removes said prompt from the stream... back to the drawing board I guess.

Re: RegEx matching on WaitFor or Prompt

by wodSupport, Friday, August 05, 2005, 22:57 (7049 days ago) @ rsg98

[code]
'Clear receive buffer
strBuffer = SshConnection.Receive
Debug.Write( *** Initial Data *** & vbCrLf & strBuffer)
[/code]

This maybe isn't enough. If you connect to really slow device, it may be possible that at a time Receive is called not yet everything arrived. I prefer to use Waitfor(prompt) after calling Connect, just to be sure...

.Prompt = myprompt-> <- Works!
.Prompt = regex:myprompt-> <- Works!
.Prompt = regex:--- more --- |myprompt-> <- No matches!

Sorry, I'm no regex guru :) We just added support for it, I cannot give you advice on what kind of expression to use.

The last one is frustrating - because without being able to do that, I can't use .Execute and I'll get back into the timing mess I was in before. The regex seems to be right according to...

I'd like to point out that Waitfor implementation is just something like this:[code]localbuffer = localbuffer + ssh1.Receive
if instr(localbuffer, 1, your_prompt ) then do something[/code]
As you can see - to implement your own WaitFor that will be smarter and capture your '-----more----' is easy. Why don't you try that?
I can't go deep into behavior of your specific case, this is up to you. I can only offer suggestions on how I would do it.

Hope it helps. Regards.