解决方案 »

  1.   

    如果只是为了在C#中实现SSH,可以考虑开源项目SSH.NET Library,不依赖第三方组件,支持同步和异步执行SSH命令,支持显示命令执行状态等信息。还支持SFTP,SCP等。具体的可以查看项目介绍。
      

  2.   

    对与你遇到的这个问题本身,原因是ssh.exe是调用了另外一个程序(plink.exe,运行ssh.exe时自动在系统盘的Windows文件夹创建的),你看到的cmd.exe中输出的是从这个程序中输出的,如果你想得到一样的结果,可以使用process.StandardInput.WriteLine(@"plink [email protected]");
      

  3.   

    using System;
    using System.IO;
    using System.Threading;
    using System.Diagnostics;namespace CmdProcess
    {
    class Program
    {
    public static void Main(string[] args)
    {
    InitProcess(@"C:\Windows\System32\cmd.exe", "", null);
    p.StandardInput.WriteLine(@"C:Windows\plink.exe [email protected]");
    Console.ReadKey(true);
    }

    private static Process p = new Process();
    private static void InitProcess(string file, string args, string dir)
    {
    try
    {
    p.StartInfo.FileName = file;
    p.StartInfo.Arguments = args;
    if (!string.IsNullOrEmpty(dir))
    {
    p.StartInfo.WorkingDirectory = dir;
    }
    p.StartInfo.RedirectStandardInput = true;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
    p.Start();

    // // Handle Standard Output
    // p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
    // p.BeginOutputReadLine();
    //
    // p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);
    // p.BeginErrorReadLine();

    new Thread(new ThreadStart(GetStdErr)).Start();
    new Thread(new ThreadStart(GetStdOut)).Start();
    new Thread(new ThreadStart(SetStdIn)).Start();
    }
    catch (Exception e)
    {
    Console.Error.WriteLine("SetStdIn: " + e.Message);
    //System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
    }
    }

    private static void CloseProcess()
    {
    p.Close();
    p.Dispose();
    }

    private static void SetStdIn()
    {
    string line = "";
    try
    {
    StreamReader reader = new StreamReader(Console.OpenStandardInput());// p.StandardOutput;
    while (!reader.EndOfStream)
    {
    line = reader.ReadLine();
    p.StandardInput.WriteLine(line);
    }
    }
    catch (Exception e)
    {
    Console.Error.WriteLine("SetStdIn: " + e.Message);
    //System.Diagnostics.Debug.Print("SetStdIn: " + e.Message);
    }
    }
    private static void GetStdOut()
    {
    string line = "";
    try
    {
    StreamReader reader = p.StandardOutput;
    while (!reader.EndOfStream)
    {
    line = reader.ReadLine();
    Console.WriteLine(line);
    //System.Diagnostics.Debug.Print(line);
    }
    }
    catch (Exception e)
    {
    Console.Error.WriteLine("GetStdOut: " + e.Message);
    //System.Diagnostics.Debug.Print("GetStdOut: " + e.Message);
    }
    }

    private static void GetStdErr()
    {
    string line = "";
    try
    {
    StreamReader reader = p.StandardError;
    while (!reader.EndOfStream)
    {
    line = reader.ReadLine();
    Console.WriteLine(line);
    //System.Diagnostics.Debug.Print(line);
    }
    }
    catch (Exception e)
    {
    Console.Error.WriteLine("GetStdErr: " + e.Message);
    //System.Diagnostics.Debug.Print("GetStdErr: " + e.Message);
    }
    }

    }
    }
      

  4.   

    谢谢你清楚的回复,我相信你的方法能解决我的问题。由于时间关系,没办法试验SSH的Library,因为我的bat里面有大量的操作中间穿插了ssh.exe和scp.exe,还有一些python的脚本,把bat里面的各部分的操作分解出来写进C#的程序里会显得很繁琐。暂时采用的方法是使用UseExecuteShell和不重定向IO,这样bat执行时是可以看见完整的ssh信息(不明白为什么这个时候却看得见)。同时在bat里面添加一些log操作,以此让C#程序监视bat的运行状态。