0

So i'm using Tamir Sharp ssh library to send some command to the remote AIX machine and fetching some response. But i"m not able to read response in the scenario where i give wrong command and the response stream is null and their is no provision to see if there is no response.

so this is the method which i'm using to send command and match the expected response.

public void RunCommand(string cmd, string expected = "", int maxTimeout = 5000)
    {
        int i = 0;
        bool foundExpected = false;

        Logger.Log(Status.Info, "Entered Command -> " + cmd);
        sshStream.Write(cmd);
        Thread.Sleep(500);
        do
        {

           lastCommand = sshStream.ReadResponse();
           Logger.Log(Status.Info, lastCommand);
            if (lastCommand.Contains(expected))
                {
                    //Logger.Log(Status.Info, "Expected output displayed:  " + expected);
                    foundExpected = true;
                    Thread.Sleep(500);
                    break;
                }

            else
            {
                i += 500;
                Thread.Sleep(500);
            }
        } while (i < maxTimeout);

        if (!foundExpected)
        {
            Logger.Log(Status.Fail, String.Format("Expected output: {0} not displayed", expected));
            throw new Exception(String.Format("Expected output: {0} not displayed", expected));
        }

    }

And these are the library method implementation for Readresponse

public string ReadResponse()
    {
        int readCount;
        StringBuilder resp = new StringBuilder();
        byte[] buff = new byte[1024];
        Match match;

        do
        {
            readCount = this.Read(buff);
            resp.Append(System.Text.Encoding.Default.GetString(buff), 0, readCount);
            string s = resp.ToString();
            match = m_prompt.Match(s);
        } while (!match.Success);

        return HandleTerminalChars(resp.ToString());
    }

public virtual int Read(byte[] buffer)
    {
        return Read(buffer, 0, buffer.Length);
    }

public override int Read(byte[] buffer, int offset, int count)
    {
        return m_in.Read(buffer, offset, count);
    }

So the problem i'm facing is when i post a wrong command it fetch the response but in next iteration of while loop it get stuck at the lastCommand = sshStream.ReadResponse() because there is no response in the stream how do i make sure to check if there is any message in the channel.

shrot
  • 145
  • 1
  • 9
  • Do not use SharpSSH. It's a dead project. – Martin Prikryl Jan 31 '18 at 13:37
  • @MartinPrikryl after going through documentation even i'm feeling the same they have not written this library properly, but the problem is most of the module i have coded using this library without thinking about the negative scenario. Anyway thanks – shrot Feb 01 '18 at 05:01

0 Answers0