Netstat聚焦于(查找端口)

本文关键字:查找 聚焦 Netstat | 更新日期: 2023-09-27 18:14:30

我最近尝试执行以下行;

string strCmdText;
strCmdText = "netstat -np TCP | find " + quote + number + quote + "";
System.Diagnostics.Process.Start("netstat.exe", strCmdText);
Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", strCmdText);

现在要做的就是找到所有包含'80'的TCP端口,并将它们显示在我定制的日志系统中,这将在我的文件夹中创建一个日志,名为;

LISTEN_TO》(80)——{date_time} . txt在这个。txt中,它应该包含命令发出的文本,但是我得到的只是一个时间。

我像上面那样调试了这个命令,不幸的是,我所知道的是CMDtext设置正确,并且我的日志系统工作正常,让我别无选择,NETSTAT可能会在查询启动后立即关闭?

希望我提供了足够的信息,因为这是我的第一篇文章。

问候,

.

由于描述模糊,这是我试图做的另一种相同的代码,但仍然只得到一个时间

const string quote = "'"";
Process p = new Process();
p.StartInfo.FileName = "cmd";
p.StartInfo.Arguments = "netstat -np TCP | find " + quote + number + quote + "";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.UseShellExecute = false;
p.Start();
String output = p.StandardOutput.ReadToEnd();
Logs.Write("LISTEN_TO(" + Registry_val1.Text + ")", output);

基本上,你可以把它看成;textbox1。文本=输出;只是现在输出被放到一个日志文件中。

Netstat聚焦于(查找端口)

我不明白你为什么要使用netstat。. net框架有很多类提供各种类型的数据,在这种情况下,IPGlobalProperties有你需要的方法。

var ip =  System.Net.NetworkInformation.IPGlobalProperties.GetIPGlobalProperties();
foreach(var tcp in ip.GetActiveTcpConnections()) // alternative: ip.GetActiveTcpListeners()
{
        if (tcp.LocalEndPoint.Port == number 
         || tcp.RemoteEndPoint.Port == number)
        {
           Logs.Write(
                String.Format(
                   "{0} : {1}", 
                   tcp.LocalEndPoint.Address, 
                   tcp.RemoteEndPoint.Address));
        }
}

使用内置类的好处是可以轻松地塑造和选择您需要的任何内容,最重要的是:您为自己和用户省去了进程外调用和输出解析。

你可以试试:

strCmdText = "cmd /c '"netstat -np TCP | find " + quote + number + quote + "'"";

如果这不起作用,请先尝试在CMD提示符中使用该命令以确保它返回数据。

cmd /c "netstat -an | find "80"