且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在C#中检测混杂的网络接口模式?

更新时间:2022-10-30 21:47:04

From *** [ ^ ]

Quote:



使用System.Management进行此类查询。 MSFT_NetAdapter类,PromiscuousMode属性。使用WMI代码创建器进行实验并获得正确的C#代码。 - Hans Passant


您可以使用 WMI 来获取MAC地址,请参阅此帖子:

C#获取计算机的MAC地址离线 - Stack Overflow [ ^ 一>

I am building an application to detect whether the network interface is in promiscuous mode or not.
We can detect it by sending a ping (ICMP echo request) with the wrong MAC address but the right IP address. If an adapter is operating in normal mode, it will drop this frame, and the IP stack never sees or responds to it. If the adapter is in promiscuous mode, the frame will be passed on, and the IP stack on the machine (to which a MAC address has no meaning) will respond as it would to any other ping.
But I don't know how to implement it in c#.

What I have tried:

I have tried this code but it gives me mac address for any particular ip address.

if (SendArp(ip, 0, macAddr, ref macAddrLen) != 0)
{
    return false;
}
else
{
   }

From ***[^]
Quote:


Use System.Management for such kind of queries. MSFT_NetAdapter class, PromiscuousMode property. Use WMI Code Creator to experiment and arrive at the correct C# code. – Hans Passant


You can use WMI to get the MAC adress(es), see this post:
C# Get Computer's MAC address "OFFLINE" - Stack Overflow[^]