【问题标题】:Launch PUTTY script in C# code [closed]在 C# 代码中启动 PUTTY 脚本 [关闭]
【发布时间】:2014-04-28 15:29:25
【问题描述】:

我需要编写一个使用 PUTTY 连接到 UNIX 服务器的 C# 代码,执行命令(例如“ls -la”),并将脚本的结果返回到 C#。
我该怎么做?

我在 C# 中使用 Process.Start 来运行 PUTTY 进程。

【问题讨论】:

标签: c# unix putty


【解决方案1】:

为了从您的 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..
    }

【讨论】:

  • 好的。以及如何将参数传递给 PUTTY ?例如,如果我想写 - putty.exe -ssh -b abc.txt mgroiser@icsl9932 .
  • ProcessStartInfo 中,使用 Arguments 属性。编辑了我的答案以包含它。
  • 好的。我的腻子位于 C:\tmp\putty.exe 。我的 abc.txt 文件也位于那里,但如果我按照你写的那样做(没有指定 abc.txt 的完整路径),它就找不到文件。我做了 var processStartInfo = new ProcessStartInfo { FileName = @"C:\tmp\putty.exe", RedirectStandardOutput = true, UseShellExecute = false, // 你必须将 ShellExecute 设置为 false ErrorDialog = false, Arguments = "-ssh -m abc.txt mgroiser@icsl9932" };
  • 我把文件放在我的 bin/debug 文件夹中只是为了测试。该过程开始,但我在 process.StarndardOutput 中没有得到任何东西。 abc.txt 文件包含“ls -la”命令
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-11-25
  • 2013-02-08
  • 2014-07-02
  • 2011-05-24
  • 1970-01-01
  • 2016-05-05
  • 1970-01-01
相关资源
最近更新 更多