如何从winfrom C#执行linux命令? [英] How to execute linux command from winfrom C# ?

查看:114
本文介绍了如何从winfrom C#执行linux命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经winform c#visual studio和一个在linux上运行的服务器。

i想从我的winform界面执行这个命令netstat -an | grep:80 | wc -l br />
但我没有输出,我不知道问题出在哪里。



我尝试了什么:



使用System.Diagnostics;

使用System.Security;



ProcessStartInfo proc_start_info = new ProcessStartInfo();

proc_start_info.FileName =bash;

proc_start_info.Arguments = - c netstat -an | grep:80 | wc - l;

// -c允许等待命令执行并退出



proc_start_info.RedirectStandardOutput = true;

proc_start_info.UseShellExecute = false;

proc_start_info.CreateNoWindow = true;



流程proc = new Process();

proc.StartInfo = proc_start_info;

proc.Start();

string r esult = proc.StandardOutput.ReadToEnd();



details_socket.Text = result;

all i have winform c# visual studio and a server running in linux.
i want to execute this command " netstat -an | grep :80 |wc -l " from my winform interface
but i have no output and i don't know where is the problem.

What I have tried:

using System.Diagnostics;
using System.Security;

ProcessStartInfo proc_start_info = new ProcessStartInfo();
proc_start_info.FileName = "bash";
proc_start_info.Arguments = "-c netstat -an | grep :80 |wc -l ";
// -c allows to wait the command to be execute and exit

proc_start_info.RedirectStandardOutput = true;
proc_start_info.UseShellExecute = false;
proc_start_info.CreateNoWindow = true;

Process proc = new Process();
proc.StartInfo = proc_start_info;
proc.Start();
string result = proc.StandardOutput.ReadToEnd();

details_socket.Text = result;

推荐答案

在您通过身份验证之前,linux机器不接受任何操作。



使用SSH库从Windows应用程序远程登录Linux机器。登录到Linux机器后,您可以将命令提交给生成的命令shell(可能是bash)。确保在完成后退出。



示例库​​:



GitHub - sshnet / SSH.NET:SSH.NET是.NET的安全外壳(SSH)库,针对并行性进行了优化。 [ ^ ]
The linux machine won't accept anything until you are authenticated.

Use an SSH library to remotely log into the Linux machine from your Windows app. After logging into the Linux machine, you can submit commands to the resulting command shell (likely bash). Be sure to log out when you're done.

Example library:

GitHub - sshnet/SSH.NET: SSH.NET is a Secure Shell (SSH) library for .NET, optimized for parallelism.[^]


我们已经解释过这个您在此问题的上一篇文章中提出的问题:运行Linux C#visual studio中的控制台命令? [ ^ ]。
We already explained this issue in your previous post of this question: Running a linux console command in C# visual studio ?[^].


这篇关于如何从winfrom C#执行linux命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆