Broken Response in Asyncronous Connection - WeOnlyDo Discussion board

Broken Response in Asyncronous Connection (wodSSH / wodSSH.NET)

by Mike D, Thursday, December 11, 2014, 16:33 (3396 days ago)

I created a Putty simulator where the user types in ssh commands and gets the ssh response displayed to a textbox control. I am having a bit of trouble with the response. The commands sometimes echo back, as well the response will return scattered. The buffer will return in pieces and not in one line. For example:

Authentic
ation Failed:

Is there any examples where the buffer is collected, until the prompt is received using the PromptReceived event?

The following what my code looks like so far.

void Init()
{
SshClient.ProxyType = SSH.ProxyTypes.ProxyWEBStandard;
SshClient.ProxyHostname = this.ProxyHostIpAddress;
SshClient.Login = this.DestinationUserName;
SshClient.Password = this.DestinationPassword;
SshClient.ProxyPort = 8989;
SshClient.Blocking = false;
SshClient.StripANSI = true;
SshClient.Protocol = SSH.SupportedProtocols.SSHAuto;
SshClient.Prompt = "regex:[\\$%#>] $";
SshClient.Timeout = 0;

SshClient.ConnectedEvent += new SSH.ConnectedDelegate(SshClient_ConnectedEvent);
SshClient.DisconnectedEvent += new SSH.DisconnectedDelegate(SshClient_DisconnectedEvent);
SshClient.DataReceivedEvent += new SSH.DataReceivedDelegate(SshClient_DataReceivedEvent);
SshClient.StateChangedEvent += new SSH.StateChangedDelegate(SshClient_StateChangedEvent);
SshClient.PromptReceivedEvent += SshClient_PromptReceivedEvent;
}

public void SendCommand(string command)
{
     if (IsConnectionValid())
     {
         SshClient.Send(command);
     }

}

void SshClient_PromptReceivedEvent(object Sender, EventArgs Args)
{
   
}

private void SshClient_DataReceivedEvent(object Sender, WeOnlyDo.Client.SSH.DataReceivedArgs Args)
  {
            try
            {
                String sshResponse;

                sshResponse = SshClient.Receive();

                // replace LF with CRLF so it's shown properly in textbox
                sshResponse = sshResponse.Replace("\n", "\r\n");

                DisplayText(sshResponse);
}

Broken Response in Asyncronous Connection

by wodSupport, Thursday, December 11, 2014, 16:37 (3396 days ago) @ Mike D

Mike,

hi. It's normal buffer is 'fragmented'. As data arrives, we provide it to the application. You cannot know how much data is sent in each packet, since this all depends on the server.

You should queue it by yourself in the buffer, and then use when it's suitable for you. That is, for example, how our WaitFor method works.

So, instead of

Debug.print Ssh1.Receive

you should do

a = a + Ssh1.Receive

and then check variable a which contains full received data for some string that should appear inside (such as command prompt).

In any case, that's normal. SSH protocol (or Telnet protocol) don't know about 'buffering' or 'command execution' or something similar. It known only about data flying over from one side to another. Higher logic (bash, command.exe, sh, etc) takes care of using it as commands and their responses.

Best regards,
Kreso