Ric
Ric

Reputation: 693

SharpSSH gets stuck in an infinite stream read in C# SSH app

Afternoon all,

I'm having a small problem with the SharpSSH library for .Net (see http://www.tamirgal.com/blog/page/SharpSSH.aspx)

        SshStream ssh = new SshStream("some ip address", "some username", "some password");
        ssh.Prompt = "\n";
        ssh.RemoveTerminalEmulationCharacters = true;            

        ssh.Write("ssh some ip address");
        // Don't care about this response
        ssh.ReadResponse();

        ssh.Write("lss /mnt/sata[1-4]");
        // Don't care about this response (for now)
        ssh.ReadResponse();

        // while the stream can be read
        while (ssh.CanRead)
        {
            Console.WriteLine(ssh.ReadResponse());
        }

        ssh.Close();

As you can see, it's fairly straight forward.

However, when the while-loop gets stepped into, it won't break out of the loop when everything has been printed to the console and there is nothing else to read.

Is there anyway I can manually force it to break when there is nothing else to be read?

Cheers, Ric

Upvotes: 3

Views: 4485

Answers (2)

Silas Nalley
Silas Nalley

Reputation: 1

while(true)                                                                            
{                                                                              
    //Write command to the SSH stream                                            
        ssh.Write( "some command or execute a script on aix" );                      
        data = "";                                                                   
        data = ssh.ReadResponse();                                                   
        if (data.Length < 10)     //don't wnat response like $                       
        continue;                                                                

        textWriter.Write(data);  //write all of data to file on each response loop   

        if (data.Contains("EOF"))    //was insert at end of file so I can terminate  
        break;                                                                      

}                                                                              
  textWriter.Close();                                                            
ssh.Close(); //Close the connection                                            
}                                                                                

Upvotes: 0

Alexander Shvetsov
Alexander Shvetsov

Reputation: 639

ssh.CanRead indicates that Stream has implementation for Read, not that it can be read.

Upvotes: 4

Related Questions