使用putty在c#中关闭linux(Using putty to shutdown linux in c#)

编程入门 行业动态 更新时间:2024-10-28 15:33:09
使用putty在c#中关闭linux(Using putty to shutdown linux in c#)

我在编写应用程序时遇到了一些问题,从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甚至没有显示出来。

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

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.

最满意答案

试试SSH.NET

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

Try SSH.NET

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

更多推荐

本文发布于:2023-07-14 23:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108389.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:putty   linux   shutdown

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!