Control a #Linux machine from C#
If your architecture involves both Windows and Linux machines, and suddenly Apache crashes, or you otherwise lose contact with the Linux machine, then normally this requires a linux admin to go in, and restart services. But, you can also have your Windows machine remotely monitor these services, and kick them off itself.
- Ok, this is a contrived situation, it’s more about if you’re a good c# programmer but are clueless around linux – like me.
Anyway, there’s a NuGET package which you can install using Install-Package SSH.NET, then you can get C# to connect to your Linux server as follows
string strOut = “”;
// Install-Package SSH.NET
using (var client = new SshClient(“112.12.12.123”, “root”, “password”))
{
client.Connect();
var result = client.RunCommand(“/etc/init.d/httpd restart”);
strOut = result.Result;
client.Disconnect();
}
Console.WriteLine(strOut);
Console.ReadLine();