C# 从Windows计算机与SSH会话交互的最简单方法?

C# 从Windows计算机与SSH会话交互的最简单方法?,c#,php,.net,windows,ssh,C#,Php,.net,Windows,Ssh,我必须在windows机器上编写一个应用程序(必须是windows,因为该机器正在做其他windows-y的事情),通过ssh同时与两个不同的Tandberg单元进行交互。观察他们的控制台日志流并对某些事件作出反应。我需要在变量中存储一些信息,并在这两个不同的ssh会话之间进行比较,或者我只需要使用一些快速的securecrt脚本 我可以让它在php(cli)中非常容易地工作——这是我知道的一种语言,但显然不适合这个项目。我很擅长通过黑客破解新语言,我想我可能可以在.net/c中实现这一点——有

我必须在windows机器上编写一个应用程序(必须是windows,因为该机器正在做其他windows-y的事情),通过ssh同时与两个不同的Tandberg单元进行交互。观察他们的控制台日志流并对某些事件作出反应。我需要在变量中存储一些信息,并在这两个不同的ssh会话之间进行比较,或者我只需要使用一些快速的securecrt脚本


我可以让它在php(cli)中非常容易地工作——这是我知道的一种语言,但显然不适合这个项目。我很擅长通过黑客破解新语言,我想我可能可以在.net/c中实现这一点——有谁有不花几百美元的首选ssh.net库吗?我也愿意接受任何其他实现这一目标的建议

在php中,您可以使用SSHv2库:

要读取控制台,请使用ssh2_fetch_stream;要执行命令,请使用ssh2_exec。

我已成功地将命令发送到Linux,并从这些命令中检索基于文本的结果

以下是您想要的全部内容:

    public string ExecuteCmd()
    {
        try
        {
            string strReturn = "";
            string param = "";
            StreamReader standerOut;

            Process p = new Process();
            p.StartInfo.FileName = @"plink.exe";

            if (this.pass.Length == 0 || this.user.Length == 0 || this.host.Length == 0 || this.cmd.Length == 0)
            {
                throw new Exception("SSHClient: Invalid parameteres for SSHClient.");
            }

            param = "-ssh -pw " + this.pass + " " + this.user + "@" + this.host + " " + this.cmd;

            string cd = Environment.CurrentDirectory;

            if (File.Exists("plink.exe") == false)
            {
                throw new Exception("SSHClient: plink.exe not found. Make sure plink.exe is in the same folder as YOUR EXE.");
            }
            else
            {
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.Arguments = param;

                p.Start();
                standerOut = p.StandardOutput;

                while (!p.HasExited)
                {
                    if (!standerOut.EndOfStream)
                    {
                        strReturn += standerOut.ReadLine() + Environment.NewLine;
                        //textBox2.SelectionStart = textBox1.Text.Length;
                        //textBox2.ScrollToCaret();
                    }

                    // Application.DoEvents();
                }                    
            }

            return strReturn;
        }
        catch (Exception exp)
        {
            throw new Exception("SSHClient:", exp);
        }
    }

我也为此使用了PLink,但如果您不想启动另一个进程,请尝试SSH.NET: