Eric Hansen Eric Hansen - 3 months ago 19
C# Question

Plink returning unwanted characters via C#

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

Command:

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


Linux result:

aaco/informatica/tgtdynamicparams2269885_CHECK_REF_COMPANY.out
cdph/informatica/tgtdynamicparams2225704_CDPHDRUGRECON.out
cdph/informatica/tgtdynamicparams2225704_CDPHELIGRECON.out
merh/informatica/tgtdynamicparams3454321_OPEN_TEST.out
merh/informatica/tgtdynamicparams3454322_OPEN_TEST2.out


C# via Plink result:

[01;32mcdph/informatica/tgtdynamicparams2225704_CDPHDRUGRECON.out[0m
[01;32mcdph/informatica/tgtdynamicparams2225704_CDPHELIGRECON.out[0m
[01;32mmerh/informatica/tgtdynamicparams3454321_OPEN_TEST.out[0m
[01;32mmerh/informatica/tgtdynamicparams3454322_OPEN_TEST2.out[0m
[0m[01;32maaco/informatica/tgtdynamicparams2269885_CHECK_REF_COMPANY.out[0m


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 ssh.net 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",
GetFileNames
};
Request(ServerName, UserName, Password, GetProcessesCommands);


Calls this:

private void Request(string remoteHost, string userName, string password, IEnumerable<string> lstCommands)
{
try
{
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)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.StackTrace);
}
}

private void AsyncReadFeedback(StreamReader strr)
{
var trdr = new Thread(ReadFeedback);
trdr.Start(strr);
}

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
Trim()
. 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.

Answer

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 username@example.com -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 username@example.com -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.