从Windows机器与SSH会话交互的最简单方法


Easiest way to interact with an SSH session from a Windows machine?

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

我可以很容易地使它在php(cli)中工作-这是我知道的一种语言,但显然不适合这个项目。我很擅长开发新语言,我想我可以用。net/c#来做——有人有不需要几百美元的首选ssh。net库吗?我也愿意接受任何其他的建议。

我已经成功地使用plink.exe向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。净:http://sshnet.codeplex.com/。

在php中可以使用SSHv2库:http://php.net/manual/en/book.ssh2.php

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