Use pcap4j to detect a mac address of remote host
I searched the whole website, and I found no solution provided to detect a mac address of remote host under same subnetwork.
So I created this project.
I used pcap4j to send and filtering packets. and for a IPv4 address, I used arp protocol related function to extract the mac address. and for a IPv6 address, I used ndp (neighbor discovery) protocol related function to extract the mac address.
- pcap4j 1.7.3
- libpcap. (ready for use version files has been uploaded into libpcapfiles)
gradle:
implementation group: 'io.gitee.gaoxingliang', name: 'mac-address-detector-java', version: '0.0.1'
mvn:
<dependency>
<groupId>io.gitee.gaoxingliang</groupId>
<artifactId>mac-address-detector-java</artifactId>
<version>0.0.1</version>
</dependency>
and use it, see full Test:
MacAddress address = MacAddressHelper.getInstance()
.getMacAddress(Inet4Address.getByName("192.168.110.100"));
the pre-built dep files are under libpcapfiles
directory.
on linux, you can load the libpcap directly without install anything
java -Dorg.pcap4j.core.pcapLibName=libpcap.so YourProgram
On windows, the npcap is required to be installed. (no reboot required)
java -Dorg.pcap4j.core.pcapLibName=wpcap.dll -Dorg.pcap4j.core.packetLibName=Packet.dll YourProgram
release the lib to mvn repo. and renaming the package.
Add a func to do packet dump by using pcap4j.
After build the jar, call the main class PacketDump.
java -Dorg.pcap4j.core.pcapLibName=libpcap.so -cp .:networkutils-all-0.2.jar PacketDump "udp"