我必须在Windows机器上编写一个应用程序(必须是Windows,因为机器正在做其他窗口事物),它们通过ssh同时与两个不同的Tandberg单元交互。观察他们的控制台日志流并对某些事件做出反应。我需要在变量中存储一些信息,并在这两个不同的ssh会话中进行比较,或者我只是使用一些快速的securecrt脚本。
我可以很容易地在php(cli)中使用它 - 这是我所知道的一种语言,但显然不适合这个项目。我非常擅长通过搞清新语言来破解我的方式,我想我可以在.net / c#中做到这一点 - 有没有人有一个首选的ssh .net库,不花几百美元?我也愿意接受任何其他建议来完成这项工作。
答案 0 :(得分:1)
我已成功使用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);
}
}
答案 1 :(得分:1)
我也使用了PLink,但是如果你想避免启动另一个进程,请尝试使用SSH.NET:http://sshnet.codeplex.com/。
答案 2 :(得分:0)
在php中,您可以使用SSHv2库:http://php.net/manual/en/book.ssh2.php
要读取控制台,请使用ssh2_fetch_stream并执行命令,请使用ssh2_exec。