sshnet / SSH.NET

SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.
http://sshnet.github.io/SSH.NET/
MIT License
3.88k stars 919 forks source link

Malformed output and paging interaction when reading stream containing paged output #1300

Closed PonchoPowers closed 5 months ago

PonchoPowers commented 5 months ago

The output is paged with one of the commands I am running on a server, and looks like so:


  19   192.168.1.30   XX-XX-XX-XX-XX-XX   RedmondWorkstation40332                  LAN12        ---    P4

  20   192.168.1.31   XX-XX-XX-XX-XX-XX   RedmondWorkstation52499               LAN12        ---    P4

--- MORE ---   ['q': Quit, 'Enter': New Lines, 'Space Bar': Next Page] ---                                                                                21   192.168.1.32   88-52-EB-26-28-2A   Redmi-10-2022            LAN1        ---    P1

--- MORE ---   ['q': Quit, 'Enter': New Lines, 'Space Bar': Next Page] --- 

I'm using a stream reader with ReadLine() to get the output, so it was my surprise when I found the paging was continuing on, without me handling it. Not only that, the output is malformed. I expected to see:

--- MORE ---   ['q': Quit, 'Enter': New Lines, 'Space Bar': Next Page] --- 
  21   192.168.1.32   XX-XX-XX-XX-XX-XX   RedmondWorkstation30111            LAN12        ---    P4

The following is the code used to read the stream after a command is written to ssh:

private string ReadStream()
{
    var reader = new StreamReader(_stream);
    var result = new StringBuilder();

    string? line;
    while ((line = reader.ReadLine()) != null)
    {
        result.AppendLine(line);
        Thread.Sleep(10);
    }

    return result.ToString();
}

Therefore, I'm not quite sure as to why the paging is continuing on when I'm reading, and secondly how the output is ending up with malformed characters and multiple lines as one line.

Any ideas what is happening here please?

PonchoPowers commented 5 months ago

Version: 2023.0.1 Platform: Windows x64 Target: .net 8

Steps to reproduce are quite specific, because you would need access to the SSH server.

PuTTY does not exhibit the same behavior.

PonchoPowers commented 5 months ago

I changed the encoding to ascii and I can see lots of backspace characters, which is very odd... image

PonchoPowers commented 5 months ago

Also, I spotted someone else who has had this issue in the past before the rewrite: https://library759.rssing.com/chan-6841305/all_p104.html

PonchoPowers commented 5 months ago

Sorry, I have fixed it by using ShellStream.Write instead.