Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling unix shell script remotely from C#

Tags:

c#

shell

unix

In my current project, i need to call a Unix shell script from the C# application. I also need to get the response back whether the script has been execute successfully or any error has occurred.

The C# program is running on a Windows machine. I need to connect to a Unix machine and execute the script.

Can anyone let me know how this can be done using C#?

like image 843
Hari KRK Avatar asked Jun 02 '11 14:06

Hari KRK


2 Answers

Will this solve your problem?

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

Update

Refer to the developer's site for SharpSSH for more information on how to use the tool.

Update 2

  • change link of developer site to archived link.
like image 138
OnesimusUnbound Avatar answered Oct 13 '22 01:10

OnesimusUnbound


A straight forward way of preforming this using 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();

like image 40
Terrance Avatar answered Oct 13 '22 01:10

Terrance