SharpSSH陷入了无限流读取C#SSH应用程序

时间:2010-03-12 15:49:56

标签: c# .net ssh sharpssh

下午所有,

我对.Net的SharpSSH库有一个小问题 (见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();

正如你所看到的,它是相当直接。

但是,当while循环进入时,当所有内容都打印到控制台并且没有其他内容可供阅读时,它不会突破循环。

无论如何,如果没有别的东西可以阅读,我可以手动强制它中断吗?

干杯, RIC

2 个答案:

答案 0 :(得分:4)

ssh.CanRead表示Stream具有Read的实现,而不是可以读取。

答案 1 :(得分:0)

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                                            
}