Eric Hansen Eric Hansen - 1 month ago 5x
C# Question

Plink returning unwanted characters via C#

I am getting unwanted characters before and after my results when using Plink via C#:


ls -l */informatica/tgtdynamicparams*.out | grep vaulttest| grep 'Sep 1'|awk '{print $9}' | sort

Linux result:


C# via Plink result:


I've tried the following without any luck:

StandardOutputEncoding = Encoding.UTF8
StandardOutputEncoding = Encoding.ASCII
StandardOutputEncoding = Encoding.DEFAULT

Here is the code I'm using. I have seen replies for similar problems suggesting use of but I am not permitted to use that it has to be Plink.

private string GetFileNames = @"ls -l */informatica/tgtdynamicparams*.out
| grep vaulttest| grep 'Sep 1'
| awk '{print $9}'
| sort";

GetProcessesCommands = new [] {
@"cd /src/trs/runjobs",
Request(ServerName, UserName, Password, GetProcessesCommands);

Calls this:

private void Request(string remoteHost, string userName, string password, IEnumerable<string> lstCommands)
var psi = new ProcessStartInfo
FileName = FormInformatiKill.PropFile.Plink,
Arguments = string.Format("-ssh {0}@{1} -pw {2}", userName, remoteHost, password),
RedirectStandardError = true,
RedirectStandardOutput = true,
RedirectStandardInput = true,
UseShellExecute = false,
CreateNoWindow = true,
StandardOutputEncoding = Encoding.UTF8

var p = Process.Start(psi);
_mObjLock = new object();
_mBlnDoRead = true;

if( p == null ) return;
AsyncReadFeedback(p.StandardOutput); // start the async read of stdout

var strw = p.StandardInput;

foreach( var cmd in lstCommands )
strw.WriteLine( cmd ); // send commands
strw.WriteLine("exit"); // send exit command at the end

p.WaitForExit(); // block thread until remote operations are done
catch (Exception e)

private void AsyncReadFeedback(StreamReader strr)
var trdr = new Thread(ReadFeedback);

private void ReadFeedback(object objStreamReader)
var strr = (StreamReader) objStreamReader;
while (!strr.EndOfStream && _mBlnDoRead)
var line = strr.ReadLine();
// lock the feedback buffer (since we don't want some messy stdout/err mix string in the end)
lock (_mObjLock)
if( line != null ) ListOfFilesResults.Add(line.Trim());

I've tried
. I even spent time just trying to remove the characters I didn't want, but there seems to be more possibilities than I can code for.


Those are ANSI escape codes.

While using --color=never may help, the root problem is that, the way you run Plink (feeding the command using a standard input), you make it use an interactive session. Most systems nowadays are configured to use a fancy formatting for directory listing (and other stuff) for interactive sessions to be more human friendly.

While in your case, no human is involved, so you should use a non-interactive session.

There are two ways to do that:

  • Either provide the command on Plink command-line (instead of feeding it using a standard input), like:

    plink.exe -ssh -pw password "ls -l ... | grep ... | sort"

    This syntax implicitly forces a non-interactive mode.

  • Or use the -T switch to explicitly force the non-interactive mode:

    plink.exe -ssh -pw password -T

    This syntax allows you to keep feeding the command using the standard input.

Using the non-interactive session (on a properly configured system) you avoid all the troubles of the interactive sessions (not just the --color). So it's a way more generic solution.

If this does not work for you, it must be because your server is misconfigured and aliases the ls to use --color even for non-interactive sessions.

If you cannot fix the server, I suggest you use both non-interactive session (to avoid other possible troubles) and the --color=never switch. Possibly better solution than using --color=never is using unalias ls.