Last active
January 7, 2018 16:00
-
-
Save kaitoy/eebcd5bdfab179cab916d3182f3d6d11 to your computer and use it in GitHub Desktop.
Packet capture with Pcap4J in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import org.pcap4j.core.BpfProgram.BpfCompileMode; | |
import org.pcap4j.core.NotOpenException; | |
import org.pcap4j.core.PacketListener; | |
import org.pcap4j.core.PcapHandle; | |
import org.pcap4j.core.PcapNativeException; | |
import org.pcap4j.core.PcapNetworkInterface; | |
import org.pcap4j.core.PcapNetworkInterface.PromiscuousMode; | |
import org.pcap4j.packet.Packet; | |
import org.pcap4j.util.NifSelector; | |
public class Pcap4jLoop { | |
public static void main(String [] args) throws PcapNativeException, IOException, NotOpenException, InterruptedException { | |
String filter = null; | |
if (args.length != 0) { | |
filter = args[0]; | |
} | |
PcapNetworkInterface nif = new NifSelector().selectNetworkInterface(); | |
if (nif == null) { | |
System.exit(1); | |
} | |
final PcapHandle handle = nif.openLive(65536, PromiscuousMode.PROMISCUOUS, 10); | |
if (filter != null && filter.length() != 0) { | |
handle.setFilter(filter, BpfCompileMode.OPTIMIZE); | |
} | |
PacketListener listener = new PacketListener() { | |
@Override | |
public void gotPacket(Packet packet) { | |
printPacket(packet, handle); | |
} | |
}; | |
handle.loop(5, listener); | |
} | |
private static void printPacket(Packet packet, PcapHandle ph) { | |
StringBuilder sb = new StringBuilder(); | |
sb.append("A packet captured at ") | |
.append(ph.getTimestamp()) | |
.append(":"); | |
System.out.println(sb); | |
System.out.println(packet); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment