在SharpPCap如何找到设备的IP地址? [英] In SharpPCap How do I find the IP Address of the Device?
问题描述
我使用的是WinPCapDevice并已初始化它。我只想能够从该设备获得IP,我找不到任何地方如何提取设备的IP地址。如果心不是一个办法做到这一点则有另一种方式来获得一个WinPCapDevice的IP地址,以便我可以检查它反对IPAddresses列表?
下面是该小块的,我讲的代码
IPHostEntry主机;
主机= Dns.GetHostEntry(Dns.GetHostName());
的foreach(在host.AddressList ip地址IP)
{
如果(ip.AddressFamily.ToString()==网间)
{
localIPAddress = ip.ToString();
//想检查我的WinPCapDevice设备的IP等于ip的
}
}
的 WinPcapDevice
类包含一个名为地址属性
。这个属性保存与设备相关联的所有地址(IP):
字符串localIPAddress =...;
WinPcapDeviceList设备= WinPcapDeviceList.Instance;
的foreach(WinPcapDevice开发中的设备)
{
Console.Out.WriteLine({0},dev.Description);
的foreach(在dev.Addresses PcapAddress地址)
{
如果(addr.Addr = NULL&放大器;!&安培;!addr.Addr.ipAddress = NULL)
{
Console.Out.WriteLine(addr.Addr.ipAddress);
如果(localIPAddress == addr.Addr.ipAddress.ToString())
{
Console.Out.WriteLine(捕捉设备中找到);
}
}
}
}
中当然,你也可以使用 CaptureDeviceList
类来获得特定设备的列表。在此列表中的每个工具设备 ICaptureDevice
。那么你必须转换为 WinPcapDevice
,一个 LibPcapLiveDevice
或 AirPcapDevice
以访问地址属性。
希望,这有助于。
I am using a WinPCapDevice and have already initialized it. I just want to be able to get the IP from that device and I cannot find anywhere how to extract the IP address of the Device. If there isnt a way to do it then is there another way to get the IP address of a WinPCapDevice so that I can check it against a list of IPAddresses?
Here is the small chunk of code that I am talking about.
IPHostEntry host;
host = Dns.GetHostEntry(Dns.GetHostName());
foreach (IPAddress ip in host.AddressList)
{
if (ip.AddressFamily.ToString() == "InterNetwork")
{
localIPAddress = ip.ToString();
//Want to check if my WinPCapDevice device's IP is equal to ip
}
}
The WinPcapDevice
class contains a property called Addresses
. This property holds all addresses (IP) associated with the device:
string localIPAddress = "...";
WinPcapDeviceList devices = WinPcapDeviceList.Instance;
foreach(WinPcapDevice dev in devices)
{
Console.Out.WriteLine("{0}", dev.Description);
foreach(PcapAddress addr in dev.Addresses)
{
if(addr.Addr != null && addr.Addr.ipAddress != null)
{
Console.Out.WriteLine(addr.Addr.ipAddress);
if(localIPAddress == addr.Addr.ipAddress.ToString())
{
Console.Out.WriteLine("Capture device found");
}
}
}
}
Of course, you could also use the CaptureDeviceList
class to get a list of specific devices. Every device in this list implements ICaptureDevice
. You then have to cast to a WinPcapDevice
, a LibPcapLiveDevice
or a AirPcapDevice
in order to access the Address property.
Hope, this helps.
这篇关于在SharpPCap如何找到设备的IP地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!