SharpSSH is stuck in an endless stream readable in a C # SSH application

Afternoon

I have a little 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, this is pretty straight forward.

However, when the while loop is turned on, it will not exit the loop when everything is printed on the console, and there is nothing more to read.

In any case, can I manually force it to break when nothing else is read?

Cheers ric

+3
source share
2 answers

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

+4
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

Source: https://habr.com/ru/post/1736682/


All Articles