plink.exe:在输出中获取无效字符

时间:2014-06-17 16:52:19

标签: c# ssh putty plink

我正在使用 plink.exe 在远程服务器上执行命令。我能够连接远程服务器但是当我执行命令到服务器时,它返回无效字符输出,如:

  

[7升[H [2J [25L [1;?1H [37米[40米[2; 1 H [3; 1 H [4; 1 H [5; 1 H [6; 1 H [7; 1 H [8; 1 H [ 9; 1 H [10; 1 H [11; 1 H
  [12; 1 H [13; 1 H [14; 1 H [15; 1 H [16; 1 H [17; 1 H [18; 1 H [19; 1 H [20; 1 H [21; 1 H [22; 1 H [23; 1 H [24 ; 1H
  [?12l [?25h [1; 1HMicrosoft Windows [Version 6.1.7601] [2; 1HCopyright(c)   2009年微软公司。所有权利   保留。[4; 1HC:\ Users \ Admin> dir / r [5;驱动器C中的1H卷是   UneqSol [6; 1H卷序列号是02F3-A323 [8; 1H目录   C:\ Users \ Admin [10; 1H06 / 17/2014 08:18 PM
  。[11; 1H06 / 17/2014 08:18 PM .. [12; 1H03 / 08/2014   上午11:39 .android [13; 1H03 / 08/2014 11:34 AM   .AndroidStudioPreview [14; 1H06 / 04/2014 09:39 PM 65   .gplink_config [15; 1H03 / 08/2014 11:26 AM   .gradle [16; 1H07 / 10/2013 07:44 PM
  .idlerc [17; 1H08 / 18/2013 03:10 PM 1,189   .opgalaxy7.vr [18; 1H03 / 11/2013 10:59 PM
  .swt [19; 1H02 / 22/2014 08:47 PM
  .VirtualBox [20; 1H07 / 06/2013 05:52 PM
  联系方式[21; 1H06 / 08/2014 02:58 PM
  桌面[22; 1H11 / 23/2013 01:37 PM
  Dir [23; 1H06 / 17/2014 09:39 PM
  文件[24; 1H06 / 17/2014 07:01 PM下载[24; 1H

我与服务器连接的代码是:

 m_Process = new Process();
 m_Process.EnableRaisingEvents = true;
 m_Process.StartInfo.FileName = "plink.exe";
 m_Process.StartInfo.Arguments = "-ssh admin@localhost -pw test";
 m_Process.StartInfo.RedirectStandardOutput = true;
 m_Process.StartInfo.RedirectStandardInput = true;
 m_Process.StartInfo.RedirectStandardError = true;
 m_Process.StartInfo.UseShellExecute = false;
 m_Process.StartInfo.CreateNoWindow = true;
 m_Process.OutputDataReceived += m_Process_OutputDataReceived;

 m_Process.Exited += delegate(object sender, EventArgs ev)
 {
     m_CloseCallback(true);
 };

 m_Process.Start();
 m_Process.BeginOutputReadLine();
 m_Process.BeginErrorReadLine();

 StreamWriter strw = m_Process.StandardInput;
 strw.WriteLine("dir /r"); // send commands 

 // Wait for application to start and become idle
 m_Process.WaitForExit();

如果收到数据,我的输出值高于

 private static void m_Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
 {
    string value = e.Data;
 }

如果有人能够识别问题,那就太棒了。

由于

1 个答案:

答案 0 :(得分:1)

尝试设置输出的编码

m_Process.StartInfo.StandardOutputEncoding = Encoding.ASCII;
m_Process.StartInfo.StandardErrorEncoding = Encoding.ASCII;

这是未经测试的。您可能需要不同的编码:http://msdn.microsoft.com/en-us/library/system.text.encoding.aspx