我必须在 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/