参考上一篇:
使用WinPcap(SharpPcap)实现ARP抓包以实现设备IP搜索功能
搜索出设备后,需要修改设备IP网关掩码等信息
继续采用ARP包
getBas类似之前的,根据用户电脑的网卡物理地址mac,还有修改后的ip, netmask, gateway发送arp包
设备接收到后解析修改即可。
//遍历网卡 foreach (var device in devices1) { if (!device.Description.ToLower().Contains("vmware") && !device.Description.ToLower().Contains("virtual"))//排除虚拟机网卡 { DeviceThread = new Thread(new ThreadStart(() => { if (device.Addresses.Count > 0) { foreach (var address in device.Addresses) { if (address.Addr.type == SharpPcap.LibPcap.Sockaddr.AddressTypes.AF_INET_AF_INET6) { if (address.Addr.ipAddress.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork) { localIP = address.Addr.ipAddress; break; } } } } foreach (var address in device.Addresses) { if (address.Addr.type == SharpPcap.LibPcap.Sockaddr.AddressTypes.HARDWARE) { localMAC = address.Addr.hardwareAddress; } } var ethernetPacket = new EthernetPacket(localMAC, pmac, EthernetPacketType.Arp); var arpPacket = new ARPPacket(getBas(mac, ip, netmask, gateway)); ethernetPacket.PayloadPacket = arpPacket; device.Open(); device.SendPacket(ethernetPacket); device.Close(); })); DeviceThread.IsBackground = true; DeviceThread.Start(); } }