开发者

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

开发者 https://www.devze.com 2022-12-22 14:56 出处:网络
Afternoon all, I\'m having a small problem with the SharpSSH library for .Net (see http://www.tamirgal.com/blog/page/SharpSSH.aspx)

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 get开发者_如何学编程s 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


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


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                                            
}                                                                                
0

精彩评论

暂无评论...
验证码 换一张
取 消