Receive problem in SSH - WeOnlyDo Discussion board

Receive problem in SSH (wodSSH / wodSSH.NET)

by Jeff, Thursday, January 12, 2006, 21:59 (6890 days ago)

Hello, I am using version 2.3.7 of your component in a C++ project. When trying to communicate with a Windows SSH server I am running into problems with Receive() in blocking mode. After I send dir >> tmp.txt I wait for 5 seconds to make sure the command works (via Sleep()). I also verified it did work by examing the tmp.txt file on the server. If I then call DataReady() it returns 0 bytes, or if I call Peek() it returns an empty string. But I know data is waiting for me to read because immediately after that I call Receive() and it gets data from the server?!?! Any idea why these methods are not working as expected.

Also, I am finding my Received() event does not get fired until I actually make the call to Receive() - this is not very helpful to me.

Finally, I have experimented with calling SetTimeout(5) before calling Receive(), which throws an exception as expected if the server does not have anything to send me, but WODSSH disconnects me! I know the session is ok, it is just that the server is sitting there idle. Is there anyway to tell WODSSH not to disconnect, just signal a timeout?

I really need to use Receive() instead of WaitFor() or ReceiveLine() since I am getting byte arrays from the server.

I must be missing something fundamental here - why does the Receive() model not seem to work?

What I need is for Receive() to support a model where I need to read a variable amount of data from the server, where the only way I know the server is done sending is when I do not receive any data for about 5 seconds or so.

I also tried Receive() in non-blocking mode, but it never returns any data.

Re: Receive problem in SSH

by wodSupport, Thursday, January 12, 2006, 23:13 (6890 days ago) @ Jeff

Jeff,

sleep API isn't good here since it blocks same thread where wodSSH lives in.

Can you zip your VC project and send it overhere, I'll test it agains our local SSH server on windows and (hopefully) see what's wrong?

You can send it at techsupport@weonlydo.com

Kreso

Re: Receive problem in SSH

by Jeff, Friday, January 13, 2006, 00:03 (6890 days ago) @ wodSupport

I sent the sample as Jeff.zip.

Any comments on what I am trying to do in general? Is there a way to suport receiving a variable amount and type of data from the server, where the only way I know the server is done is if I do not receive anything for a few seconds?


Thanks

Re: Receive problem in SSH

by wodSupport, Friday, January 13, 2006, 00:51 (6890 days ago) @ Jeff

Jeff,

ok, send you code back.

Your problem is that you blindly send commands. When you login to remote system, you get command prompt, right? You don't know what this prompt look like, but we do have nice regular expression (wodSSH supports that) that catches most command prompts. So I used it.

Look at new sample. When you connect, you wait for command prompt. Then you send command, and then wait for new command prompt. Everything between your Send and Waitfor is returned as result of Waitfor - so you print it.

Hope change helped!
Kreso

Re: Receive problem in SSH

by Jeff, Friday, January 13, 2006, 18:01 (6889 days ago) @ wodSupport

Kreso, I tried your sample against your server and it seems your SSH service is not running (I can ping the machine, but not connect using SSH client).

Anyway, testing locally using WaitFor() with regex:[\$ #>] $ as the first parameter (as you have) did not work - I am not regex guru but I thought it would find the DOS prompt ( > ). After I changed the first parameter to > it then gets data back ok.

But the bigger picture here is that I can't really use WaitFor() because the systems I am talking to sometimes do not return any sort of expected prompt in some cases - they could return error strings or messages indicating they are performing some task where I have to wait until I get data to know when it is done. There are too many cases to be covered by regex's.

So I was hoping to use Receive() with a small timeout, but if it times out then WODSSH will internally disconnect the session!! Is there any way to timeout without disconnecting, so I can call Receive() again.

And why does Peek() never return any data, and DataReady() always return 0? This doesn't make sense because if I then immediately do a call to Receive() it gets data.

It seems having the WODSSH object in the same thread as the main program does not really work as expected - it seems you need some sort of function that client code can call to tell the WODSSH object to process events (such as reading data into buffers, updating the data ready count, etc).

I have asked these questions in my previous posts but you did not address them - can you please give some insight?

Re: Receive problem in SSH

by wodSupport, Friday, January 13, 2006, 20:18 (6889 days ago) @ Jeff

Jeff,

our waitfor under the hood isn't that smart at all. It's loop of Receive calls until expected string arrives. You can make your own (better) function like that if you need it a bit different.

So I was hoping to use Receive() with a small timeout, but if it times out then WODSSH will internally disconnect the session!! Is there any way to timeout without disconnecting, so I can call Receive() again.

No, wodSSH will not internally disconnect the session. Hmm. Let me think.. It should not. Timeout expires, I agree, and Timeout property is there for auto-disconnect for inactivity.. But.. Hmm.. Not sure, I would have to check this.


And why does Peek() never return any data, and DataReady() always return 0? This doesn't make sense because if I then immediately do a call to Receive() it gets data.

I don't know, I didn't personally try this so I cannot answer. If something is in the buffer it would be returned. If it's not, Peek will return immediately. Receive is a blocking method, so it will actually wait for something to arrive - perhaps this is a matter of (milli)seconds but enough to get unexpected resuts.

It seems having the WODSSH object in the same thread as the main program does not really work as expected - it seems you need some sort of function that client code can call to tell the WODSSH object to process events (such as reading data into buffers, updating the data ready count, etc).

I have asked these questions in my previous posts but you did not address them - can you please give some insight?

Sorry, I somehow missed them. I'll answer now.

Re: Receive problem in SSH

by wodSupport, Friday, January 13, 2006, 20:25 (6889 days ago) @ wodSupport

Also, I am finding my Received() event does not get fired until I actually make the call to Receive() - this is not very helpful to me.

This should not be happening. However, if you call Sleep and have blocked the thread, then it makes sense since event wasn't fired at all - since nothing was processed due to Sleep.

Finally, I have experimented with calling SetTimeout(5) before calling Receive(), which throws an exception as expected if the server does not have anything to send me, but WODSSH disconnects me!

Ok, this sounds strange, I'll check it out on next few hours. It makes sense to happen due to programming logic (timeout, set by timeout property, did expire...) but doesn't make sense from end-user logic - so this probably needs to be fixed.

I really need to use Receive() instead of WaitFor() or ReceiveLine() since I am getting byte arrays from the server.

WaitFor change will change today also. If it expires, due to timeout, it will not consume everything from the buffer, like it did so far. Perhaps that will help?

What I need is for Receive() to support a model where I need to read a variable amount of data from the server, where the only way I know the server is done sending is when I do not receive any data for about 5 seconds or so.

Ok, so setting Timeout = 5, calling Receive, getting error but NOT disconnecting is the behaviour you expect? Me too - I'll check it out to make it happen.

Re: Receive problem in SSH

by Jeff, Friday, January 13, 2006, 23:10 (6889 days ago) @ wodSupport

Thanks, your new dll fixed dropping the connection when Receive() times out!!