简体   繁体   中英

Using putty to shutdown linux in c#

I encountered some problems during writing application to shutdown/restart linux from windows in c#.

I'm trying to use putty to do this task, but something is wrong.

            Process p = new Process();
            ProcessStartInfo info = new ProcessStartInfo();
            info.FileName = "putty.exe";
            info.RedirectStandardInput = true;
            info.UseShellExecute = false;
            info.Arguments = @"root@192.168.0.25 -pw 1234qwer ";

            p.StartInfo = info;
            p.Start();

            p.StandardInput.WriteLine("shutdown -h ");
            p.StandardInput.WriteLine("exit");
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
            Console.WriteLine(output);

Above I've pasted code that I've used to achieve this goal. Everything is fine till I have to write something more in putty command line, in this case StandardInput is not a good way to do this and I didn't find other way to do this.

I also tried to use PLINK.exe in the same way but it also didn't solve my problems - in fact PLINK does not even show up.

Any ideas or tips how to solve this problem would be great.

Try SSH.NET

using (var client = new SshClient("hostnameOrIp", "username", "password"))
{
    client.Connect();
    client.RunCommand("shutdown -h now;systemctl poweroff");
    client.Disconnect();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM