【问题标题】:Calling unix shell script remotely from C#从 C# 远程调用 unix shell 脚本
【发布时间】:2011-06-02 14:52:52
【问题描述】:

在我当前的项目中,我需要从 C# 应用程序调用 Unix shell 脚本。我还需要获取脚本是否已成功执行或是否发生任何错误的响应。

C# 程序正在 Windows 机器上运行。我需要连接到 Unix 机器并执行脚本。

谁能告诉我如何使用 C# 来做到这一点?

【问题讨论】:

  • 你在 Unix 机器上运行 C# 程序吗?
  • C# 程序在 windows 机器上。我需要连接到 unix 机器并执行脚本。

标签: c# shell unix


【解决方案1】:

这会解决您的问题吗?

sharpSsh - A Secure Shell (SSH) library for .NET

更新

有关如何使用该工具的更多信息,请参阅开发者网站SharpSSH

更新 2

  • 将开发者网站的链接更改为存档链接。

【讨论】:

  • 我已经检查过了。这似乎很有希望。但正如作者所说,它并不完整。连接到远程机器时出现错误。我们正在努力解决这个问题。
  • 您好,我们试过了,但无法解决问题。连接到远程机器时出现超时错误。
  • 您好,我们试过了,但无法解决问题。连接到远程机器时出现超时错误。
  • 您好,您访问过sharpSsh的作者site
  • 直接使用作者网站的源代码为我们工作。非常感谢您的帮助。
【解决方案2】:

使用System.Diagnostics.Process 进行此操作的直接方式

// Start the child process. Process p = new Process(); // Redirect the error stream of the child process. p.StartInfo.UseShellExecute = false; p.StartInfo.RedirectStandardError = true; p.StartInfo.FileName = "Write500Lines.exe"; p.Start(); // Do not wait for the child process to exit before // reading to the end of its redirected error stream. // p.WaitForExit(); // Read the error stream first and then wait. string error = p.StandardError.ReadToEnd(); p.WaitForExit();

【讨论】:

  • 谢谢特伦斯。 C# 应用程序在 Windows 环境中运行,我需要连接到远程 unix 框以在那里执行 shell 脚本。有什么直接的方法吗?
【解决方案3】:

即使我遇到了同样的问题,我已经用谷歌搜索了大约 1 个月的解决方案。

最后,我决定使用 plink.exe(putty.exe 的命令行版本)连接到 unix box 并在那里执行脚本。

您必须通过 c# 进程使用 plink,我已经尝试过了,效果非常好。

但是现在我面临的问题是,当我从 c# 进程运行脚本时,我无法将参数传递给该脚本。说我不知道​​该怎么做可能是一种习惯。

问候 -阿卡什

【讨论】:

  • 嗨,Aakash,正如 OnesimusUnbound 所说,从原始站点查看 sharpSSH,tamirgal.com/blog/page/SharpSSH.aspx
  • 是的 Hari,但我喜欢 plink 而不是 SharpSSH,plink 更稳定。
猜你喜欢
  • 2016-06-22
  • 1970-01-01
  • 2018-02-13
  • 2013-07-09
  • 1970-01-01
  • 1970-01-01
  • 2014-09-29
  • 2010-09-21
  • 2015-02-25
相关资源
最近更新 更多