捕获netstat的输出 [英] Capturing the output of netstat
本文介绍了捕获netstat的输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为一个故障排除工具,我正在开发一个C#类,它将运行netstat,捕获输出,处理它,并启动用户关于端口冲突退出的地方。 我遇到的问题是我只得到标题输出。 这里
是完整的代码,我缺少什么?
As a trouble shooting tool, I am working on a C# class that will run netstat, capture the output, process it, and enlighten users as to where port conflicts exits. The problem I am running into is that I am only getting the header output. Here is the complete code, what am I missing?
public class NetstatMgr
{
private List<string> _netstatLines = new List<string>();
public NetstatMgr()
{
ProcessStartInfo psi = new ProcessStartInfo("netstat", "-nop");
psi.CreateNoWindow = true;
psi.ErrorDialog = false;
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardError = true;
psi.Verb = "runas";
psi.WindowStyle = ProcessWindowStyle.Hidden;
Process process = new Process();
process.EnableRaisingEvents = true;
process.StartInfo = psi;
process.ErrorDataReceived += (s, e) => { _netstatLines.Add(e.Data); };
process.OutputDataReceived += (s, e) => { _netstatLines.Add(e.Data); };
process.Start();
process.BeginErrorReadLine();
process.BeginOutputReadLine();
process.WaitForExit();
// process.Exited += process_Exited;
//}
//void process_Exited(object sender, EventArgs e)
//{
Debug.WriteLine("Done!");
foreach (var s in _netstatLines)
Debug.WriteLine(s);
}
}
推荐答案
嗨EHCarleton,
感谢您发帖。
检查完你的帖子后,你想要吗?获取端口使用率并阻止客户端用户在本地或远程网络中设置重复端口?
我建议您使用以下方法获取端口列表。
I suggest you can use the following method to get the port list.
IPGlobalProperties ipProperties = IPGlobalProperties.GetIPGlobalProperties();
IPEndPoint[] endPoints = ipProperties.GetActiveTcpListeners();
TcpConnectionInformation[] tcpConnections = ipProperties.GetActiveTcpConnections();
foreach (TcpConnectionInformation info in tcpConnections)
{
Console.WriteLine("Local: " + info.LocalEndPoint.Address.ToString()
+ ":" + info.LocalEndPoint.Port.ToString()
+ "\nRemote:" + info.RemoteEndPoint.Address.ToString()
+ ":" + info.RemoteEndPoint.Port.ToString()
+ "\nState :" + info.State.ToString() + "\n\n");
}
这篇关于捕获netstat的输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文