EldoS | Feel safer!

Software components for data protection, secure storage and transfer

ElSimpleSSHClient Commands property

Also by EldoS: CallbackDisk
Create virtual disks backed by memory or custom location, expose disk images as disks and more.
#19485
Posted: 03/16/2012 07:30:18
by Mohamed Koker (Priority Standard support level)
Joined: 01/22/2008
Posts: 18

Hi Folks,

I am trying to use the ElSimpleSSHClient to connect to a server, issue a number of commands and then disconnect.

I thought the easiest way to do this would be for me to add the commands to the Commands property and then call the Open method.

However, when I try this using the sample code, I always get an error saying the client was disconnected.

This does not occur if I do not use the Commands property and issue the same commands via the Send method after calling the Open method.

I believe this issue has come up before and the previous customer suggested that a sample illustrating the use of the Commands property would be very useful.

Regards

-Mohamed
#19499
Posted: 03/16/2012 08:22:53
by Eugene Mayevski (EldoS Corp.)

What exactly sample code are you trying?

Commands property works exactly like you described - several commands are executed and then connection is closed.


Sincerely yours
Eugene Mayevski
#19500
Posted: 03/16/2012 09:36:11
by Mohamed Koker (Priority Standard support level)
Joined: 01/22/2008
Posts: 18

Thanks Eugene,

I am referring to the SimpleSSHDemo project in the samples.

On a related topic, can you please let me know how I can eliminate the use of a timer in this sample?

I usually try to get things working the way I want in a sample before moving this to a custom class.

Regards

-Mohamed
#19501
Posted: 03/16/2012 09:45:11
by Eugene Mayevski (EldoS Corp.)

SimpleSSHDemo was not crafted for Commands property, that's why you get disconnected error. I'll try to make a quick sample for you now.

SSH is a protocol where the data can come at any moment of time. So you need to poll the channel in some way. Timer is the simplest way, though if you have some kind of message loop, you can call SSHClient.CanReceive() there. You can call CanReceive() and Sleep() in a loop, but this would work only in non-UI thread (in UI thread this would block your UI).

With Commands property, though, timer is not needed (IIRC).


Sincerely yours
Eugene Mayevski
#19502
Posted: 03/16/2012 10:28:02
by Eugene Mayevski (EldoS Corp.)

I've put the updated CmdSSHDemo sample for C# (it existed for some time but was not included into the installation for some reason) here. The sample will be included into SecureBlackbox 10, so this link will not be actual with SBB 10 release.


Sincerely yours
Eugene Mayevski
#19503
Posted: 03/16/2012 11:17:52
by Mohamed Koker (Priority Standard support level)
Joined: 01/22/2008
Posts: 18

Thanks Eugene!

Unfortunately, I still get connection lost due to error 110.

I noticed that this sample also uses a timer.

Can I remove this?

-Mohamed
#19507
Posted: 03/16/2012 11:58:45
by Eugene Mayevski (EldoS Corp.)

It's possible that the problem is on the server side -- I tested the sample before posting it and it doesn't (and never did) show any errors.

With 2 commands issued, do you get response for both or disconnection happens after the first one?

Timer can't be removed from the sample for the reason I described - something should be used to poll the incoming data. If we replace the timer with some loop, we'll lose GUI responsiveness. If you need this functionality in non-UI code, that's a different story. In this case see ConsoleSSH sample.


Sincerely yours
Eugene Mayevski
#19508
Posted: 03/16/2012 12:38:30
by Mohamed Koker (Priority Standard support level)
Joined: 01/22/2008
Posts: 18

Thanks Eugene,

I think the reason this is failing is because the server does not support non-interactive sessions.

We are investigating the feasibility of getting the vendor to change this.

In the mean time, I will try to support this with interactive sessions.

Regards

-Mohamed
#19525
Posted: 03/19/2012 11:25:11
by Mohamed Koker (Priority Standard support level)
Joined: 01/22/2008
Posts: 18

Hi Eugene,

I am still having some problem with this due to the use of the timer.

In order to access this server, I need to send commands with an interactive SSH session.

I can do this using the Send command.

However, I then need to parse the resulting feedback before I issue the next command and this is where I am having a problem.

I cannot figure out how to capture the resulting feedback after a call to the Send method.

Regards

-Mohamed
#19526
Posted: 03/19/2012 13:04:21
by Eugene Mayevski (EldoS Corp.)

Call CanReceive() method to check if there's anything to receive, then call ReceiveData() to actually receive the data.
Please note that it's possible that you have the unread data in the buffer and connection is already closed. So your simple loop will look like (in pseudo-C#)

Code
while (true)
{  
  if (SshClient.CanReceive(0))
    SshClient.ReceiveData(...);

  if (!SshClient.Active)
    break;
}


Note that this will block your UI.

Also this code is good for one command. For multiple commands you will probably need to put it to separate method and call that method for each command. This will look like

Code
bool PerformSendRecv(string command)
{
  while (true)
  {  
    if (SshClient.CanReceive(0))
      SshClient.ReceiveData(...);
    // analyze data here
    if (responseComplete)
       return true;
    else
    if (!SshClient.Active)
      return false;
  }

  ...
  while (true)
  {
    if (!PerformRecvSend(Command1)
      break;
    if (!PerformRecvSend(Command2)
      break;
    if (!PerformRecvSend(Command3)
      break;
    ...
    break; // all commands sent
  }
}


Sincerely yours
Eugene Mayevski
Also by EldoS: Callback File System
Create virtual file systems and disks, expose and manage remote data as if they were files on the local disk.

Reply

Statistics

Topic viewed 1598 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!