【问题标题】:Using putty to shutdown linux in c#c#中使用putty关闭linux
【发布时间】:2016-05-05 18:35:00
【问题描述】:

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

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

            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 甚至没有出现。

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

【问题讨论】:

    标签: c# linux putty shutdown plink


    【解决方案1】:

    试试SSH.NET

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

    【讨论】:

    • 太棒了!这就是我一直在寻找的图书馆,谢谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多