2013年2月24日星期日

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


I have to write an app on a windows machine (must be windows because the machine is doing other windows-y things) that interacts with two different Tandberg units via ssh simultaneously. Watching their console log stream and reacting to certain events. I'll need to store some information in variables and compare across these two different ssh sessions or I'd just use some quick securecrt scripting.

I could make it work in php(cli) pretty easily - it's one language I know, but clearly not ideal for this project. I'm pretty good at hacking my way through figuring out new languages, I figure I could probably do it in .net/c# - does anyone have a preferred ssh .net library that doesn't cost several hundred dollars? I'm open to any other suggestions to accomplishing this as well.

答案1:
In php you can use the SSHv2 library: http://php.net/manual/en/book.ssh2.php

To read console use ssh2_fetch_stream and to execute commands use ssh2_exec.


答案2:
I have successfully used plink.exe to send commands to Linux and retrieved text based results from those commands.

Following is all you want:

    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);
        }
    }


答案3:
I've also used PLink for this, but if you want avoid having to launch another process, try SSH.NET: http://sshnet.codeplex.com/.

没有评论:

发表评论