c# - 从 Windows 机器与 SSH session 交互的最简单方法?

标签 c# php .net windows ssh

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

我可以很容易地让它在 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);
        }
    }

关于c# - 从 Windows 机器与 SSH session 交互的最简单方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8585917/

相关文章:

php - 如何实现mysql表行的锁定机制以避免竞争条件?

c# - 将 C# 中的 tStringList 传递给 Delphi DLL

c# - 有没有办法自定义 Thinktecture.IdentityServer.v2 登录页面?

c# - 如何本地化 asp.net mvc 3 应用程序

c# - 混合 c# 和 vc++

php - MySQL 查询 PHP 和 PDF 仅显示第一个字符

php - Laravel 5.x 如何创建具有外键约束的新用户?

.net - .NET 中的自定义 ORM

c# - 异步一直向下问题

c# - 命名空间在类库 c# 中不起作用