【发布时间】:2014-04-28 15:29:25
【问题描述】:
我需要编写一个使用 PUTTY 连接到 UNIX 服务器的 C# 代码,执行命令(例如“ls -la”),并将脚本的结果返回到 C#。
我该怎么做?
我在 C# 中使用 Process.Start 来运行 PUTTY 进程。
【问题讨论】:
我需要编写一个使用 PUTTY 连接到 UNIX 服务器的 C# 代码,执行命令(例如“ls -la”),并将脚本的结果返回到 C#。
我该怎么做?
我在 C# 中使用 Process.Start 来运行 PUTTY 进程。
【问题讨论】:
为了从您的 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..
}
【讨论】:
ProcessStartInfo 中,使用 Arguments 属性。编辑了我的答案以包含它。