Search code examples
c#.netbashsshxterm

Interpreting SSH output


I am trying to create a SSH client for my personal use using the Renci SSHNet library, but I am completely unsure of how I am supposed to interpret the output from executed commands in the terminal.

For example, if I were to type simple commands like "ls -l", "cd /root", or "dir", the output strings are displayed in my Windows Console window as expected.

But when I am trying to say edit a file using "nano" or look at the cpu/memory usage using "top", basically anything that's supposed to display static text, I get a bunch of blob from the ssh host like this.

←[?1049h←[1;24r←(B←[m←[4l←[?7h←[?12l←[?25h←[?1h←=←[?1h←=←[?1h←=←[39;49m←[39;49m←
(B←[m←[H←[2J←(B←[0;7m  GNU nano 2.2.6                File: test
←[23d^G←(B←[m Get Help  ←(B←[0;7m^O←(B←[m WriteOut  ←(B←[0;7m^R←(B←[m Read File
←(B←[0;7m^Y←(B←[m Prev Page ←(B←[0;7m^K←(B←[m Cut Text  ←(B←[0;7m^C←(B←[m Cur Po
←[24d←(B←[0;7m^X←(B←[m Exit←[14G←(B←[0;7m^J←(B←[m Justify   ←(B←[0;7m^W←(B←[m Wh
ere Is  ←(B←[0;7m^V←(B←[m Next Page ←(B←[0;7m^U←(B←[m UnCut Text←(B←[0;7m^T←(B←[
←[3d Spell

So I guess my question is how I am supposed to interpret all of this? I just need someone to point me in the right direction.


Solution

  • When a program displays "static text" or is otherwise exercising control over the whole terminal display, it is actually printing commands to the terminal using hidden control characters (like those shown in your blob). See answer here. Libraries like ncurses provide a portable API for terminal control by getting information about the host terminal from terminfo.

    In your project, I suppose you'll have to obtain some kind of dictionary of the host's command sequences and filter or interpret them to display properly when printed to the client's terminal. You might take a look at the colorama project, which translates ANSI command sequences into Win32 calls to properly control a Windows command terminal. But that's for Python, which I know nothing about.