title: Plink returning unwanted characters via C#
body: <p>I am getting unwanted characters before and after my results when using Plink via C#:</p>
  
  <p>Command:</p>
  
  <pre class="lang-none prettyprint-override"><code>ls -l */informatica/tgtdynamicparams*.out | grep vaulttest| grep 'Sep  1'|awk '{print $9}' | sort
  </code></pre>
  
  <p>Linux result:</p>
  
  <pre class="lang-none prettyprint-override"><code>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
  </code></pre>
  
  <p>C# via Plink result:</p>
  
  <pre class="lang-none prettyprint-override"><code>[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
  </code></pre>
  
  <p>I've tried the following without any luck:</p>
  
  <pre><code>StandardOutputEncoding = Encoding.UTF8
  StandardOutputEncoding = Encoding.ASCII
  StandardOutputEncoding = Encoding.DEFAULT
  </code></pre>
  
  <p>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.</p>
  
  <pre><code>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);
  </code></pre>
  
  <p>Calls this:</p>
  
  <pre><code>private void Request(string remoteHost, string userName, string password, IEnumerable&lt;string&gt; 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 &amp;&amp; _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());
          }
      }
  }
  </code></pre>
  
  <p>I've tried <code>Trim()</code>. 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. </p>