Search code examples
vb.netsshstreamreaderssh.net

ShellStream output contains unknown characters


I am using Ssh.Net and a StreamReader on its ShellStream to read output from an ssh console. If you are familiar with console output, you surely know about typically table formatted data. Such as:

COLUMN1     COLUMN2     COLUMN3

value1      value2      value3

value4      value5      value6

value7      value8      value9

I am also reading the output directly via an SshCommand object and I dont have any formatting errors there. However when I am using StreamReader I get cryptic characters such as [m[1mX[m. The X is a single character, normally surrounded by spaces. This happens only next to single characters surrounded by multiple spaces. I also have this issue when characters are surrounded by brackets which results in something like [9999 or [6n

I will parse the result anyway, so I don't mind the characters, but I need to know wether they actually mean something and if they are consistent. If it is neither of that, I would be nice if you could tell me how I can fix this issue.

If it matters, I am connected to a MikroTik Router and I run the command certificate print detail without-paging. This error occurs in the header and I am using this code to output acquire a result and output the stream:

Dim response As String = ""
Dim reader As StreamReader = New StreamReader(shellStream)
Dim writer As StreamWriter = New StreamWriter(shellStream)

writer.WriteLine(cmd)
writer.Flush()

Thread.Sleep(500)

Dim line As String = ""
For i As Integer = 0 To 150

    response += line
    line = reader.ReadLine
    response += vbCrLf

Next

Console.WriteLine(response)

Solution

  • Those are ANSI Escape Sequences - namely (replace X with correpsonding number):

    [Xm - Text graphics mode (color, thickness, etc.)

    [XB - Cursor down by X

    [Xn - Probably new line

    For more information look here, here or here

    If it is of any interest: In my case I can replace every [Xm sequence because I am not using any of the given text formatting