我需要编写一个C#代码,它使用PUTTY连接到UNIX服务器,执行命令(例如“ls -la”),然后将脚本的结果返回给C#。 我该怎么办?
我在C#中使用Process.Start来运行PUTTY进程。
答案 0 :(得分:2)
为了从您的Putty流程中获取结果,您需要的是重定向流程stdout (Standard Output)
流并将其用于您的代码中:
var processStartInfo = new ProcessStartInfo
{
FileName = @"C:\PuttyLocation",
Arguments = @"-ssh -b abc.txt"
RedirectStandardOutput = true,
UseShellExecute = false, // You have to set ShellExecute to false
ErrorDialog = false
};
var process = Process.Start(processStartInfo);
if (process == null)
{
return;
}
var reader = process.StandardOutput;
while (!reader.EndOfStream)
{
// Read data..
}