标签:
1. 获取网络接口列表
import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
public class TCPCollection {
public static void main(String[] args){
NetworkInterface[] devices = JpcapCaptor.getDeviceList();//获取网络接口列表
for(int i = 0; i < devices.length; i++){
//名称、描述
System.out.println(i + ":" + devices[i].name + "(" + devices[i].description + ")");
//数据链路层名称、描述
System.out.println("datalink:" + devices[i].datalink_name + "(" + devices[i].datalink_description + ")");
//MAC地址
System.out.print(" MAC address:");
for(byte b: devices[i].mac_address){
System.out.print(Integer.toHexString(b & 0xff) + ":");
}
System.out.println();
//IP地址、子网掩码、广播地址
for(NetworkInterfaceAddress a : devices[i].addresses){
System.out.println(" address: " + a.address + "|" + a.subnet + "|" + a.broadcast);
}
}
}
}
public static void main(String[] args){
NetworkInterface[] devices = JpcapCaptor.getDeviceList();//获取网络接口列表
int index = 0;
try {
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
} catch (IOException e) {
e.printStackTrace();
System.out.println("抓取数据包时出现异常!!!");
}
}
名称 目的 NetworkInterderface 要打开的网络接口。 intrface int
snaplen 一次捕获数据包的最大byte数。 boolean
prommics 是否采用混乱模式 混乱模式中,可以捕获所有数据包,即便源MAC或目的MAC地址与打开的网络接口的MAC地址不相同。 而非混乱模式中只能捕获由宿主机发送和接收的数据包。 int
to_ms 捕获的数据包的超时设置(数量级为毫秒)。 |
class PacketPrinter implements PacketReceiver {
//this method is called every time Jpcap captures a packet
public void receivePacket(Packet packet){
System.out.print(packet);
}
}
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.processPacket(2, new PacketPrinter());
captor.close();
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
for(int i=0; i<10; i++){
System.out.println(i + ":" + captor.getPacket());
}
captor.close();
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
captor.setFilter("ip and tcp", true);
JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535, false, 20);
JpcapWriter writer = JpcapWriter.openDumpFile(captor, "yourfilename");
for(int i=0; i<10; i++){
Packet packet = captor.getPacket();
writer.writePacket(packet);
}
writer.close();
标签:
原文地址:http://www.cnblogs.com/shy-huiying/p/5636274.html