使用 putty 在 c# 中关闭 linux

本文关键字:linux putty 使用 | 更新日期: 2023-09-27 18:32:13

我在编写应用程序以从 c# 中的 windows 关闭/重新启动 Linux 时遇到了一些问题。

我正在尝试使用腻子来完成这项任务,但出了点问题。

            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);

上面我粘贴了用于实现此目标的代码。一切都很好,直到我不得不在 putty 命令行中编写更多内容,在这种情况下,StandardInput 不是执行此操作的好方法,我没有找到其他方法。

我也尝试以同样的方式使用 PLINK.exe但它也没有解决我的问题——事实上 PLINK 甚至没有出现。

如何解决此问题的任何想法或提示都会很棒。

使用 putty 在 c# 中关闭 linux

尝试 SSH.NET

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