EldoS | Feel safer!

Software components for data protection, secure storage and transfer

Using CanReceive(-1)

Also by EldoS: CallbackFilter
A component to monitor and control disk activity, track file and directory operations (create, read, write, rename etc.), alter file data, encrypt files, create virtual files.
#14640
Posted: 10/01/2010 00:35:04
by Vincent Parrett (Standard support level)
Joined: 01/17/2008
Posts: 20

Hi,
My application needs to interactively send multiple commands using a single SSH connection, wait for each command to execute and retrieve its output.

Currently, I'm using
Code
                client.Open();
              
                client.SendText("echo foo\n");
                string result = "";
                while (client.CanReceive(timeout))
                {                  
                    result += client.ReceiveText();
                }


This is fine, except that CanRecieve doesn't wait for the process to finish - it just checks if there's data after the timeout period.

I've tried CanReceive(-1), but this never returns (which makes some sense to me).

What's the best way to know when the command has finished executing? I'm aware that I could use the Command/Commands properties, however I need show the output for each command individually, and as per [URL=http://www.eldos.com/forum/read.php?FID=7&TID=2497]this thread[/URL] the OnSendCOmmandRequest event doesn't seem to always be fired.

Thanks
#14646
Posted: 10/01/2010 15:25:21
by Ken Ivanov (EldoS Corp.)

Thank you for contacting us.

The start of every new command is *always* indicated with OnSendCommandRequest call (you handle it in a bit incorrect way according to the code snippets you have provided by the topic referenced). To retrieve commands output correctly, you should
1) call CanReceive()/ReceiveText() in a loop after the Open() call,
2) *do not* call them from inside the OnSendCommandRequest handler,
3) treat OnSendCommandRequest invocation as the end of output corresponding to one command and start of output of the next command.
#14660
Posted: 10/04/2010 19:54:43
by Vincent Parrett (Standard support level)
Joined: 01/17/2008
Posts: 20

Hi Innokentiy,
Thanks for the response - I can now handle OnSendCommandRequest as I expected, and can match up a command with its response.

This works fine for a batch session, however it doesn't help me with my main problem, at least not that I can see.

My client application allows users to specify a series of commands that may have conditional logic between them. For example
Code
Open Connection
Execute Commmand One
if (outputFromCommandOne) = 'foo'
  Execute Command Two
else
  Execute Command Three
Close Connection


This is why I'm currently using SendText() - because it allows the session to stay open between commands. The problem I have is that there's no way that I can see to know when a command has completed when using SendText().

Is it possible to do what I want using the SimpleSSHClient, or do I need to look at using the full SSHClient?

Cheers.
#14661
Posted: 10/04/2010 22:54:27
by Vincent Parrett (Standard support level)
Joined: 01/17/2008
Posts: 20

As a follow-up: I started looking at SSHClient but it appears to have a similar implementation: when I create a SSHCommandTunnel the command is executed when the Client is opened, after which the tunnel is closed. I could use an SSHShellTunnel, but then (as with SendText() in SimpleSSHClient) I need to parse the terminal output.

Is there something obvious that I'm missing, that would allow me send commands while keeping the connection/tunnel open?

Cheers
#14664
Posted: 10/05/2010 07:35:53
by Ken Ivanov (EldoS Corp.)

TElSimpleSSHClient will not help if you need to implement conditional logic, as it only supports predefined batches of commands (and your task requires to modify command sequence at the time of command execution).

To resolve your task with TElSSHClient, you have to create an individual command tunnel for every subsequent command and open it with the Open() method.
Also by EldoS: CallbackRegistry
A component to monitor and control Windows registry access and create virtual registry keys.

Reply

Statistics

Topic viewed 1090 times

Number of guests: 1, registered members: 0, in total hidden: 0




|

Back to top

As of July 15, 2016 EldoS Corporation will operate as a division of /n software inc. For more information, please read the announcement.

Got it!