如何在java中使用jpcap捕获ip地址

时间:2012-10-10 08:06:09

标签: capture packet icmp jpcap

以下是捕获ICMP数据包并存储在txt文件中的代码,但存储信息的格式为二进制。任何人都可以告诉我,如何在明文文件或db文件中捕获ICMP数据包的源地址和大小[如果可能的MAC地址]进行处理。

import java.net.InetAddress;
import jpcap.packet.*;
import jpcap.*;
import jpcap.packet.EthernetPacket;
import jpcap.packet.IPPacket;
import jpcap.packet.TCPPacket;
import java.util.Scanner;

class capture
{
        public static void main(String[] args) throws java.io.IOException{
        //Get the Device information - Start

            //Obtain the list of network interfaces
            NetworkInterface[] devices = JpcapCaptor.getDeviceList();

            //for each network interface
            for (int i = 0; i < devices.length; i++) {
              //print out its name and description
              System.out.println(i+": "+devices[i].name + "(" + devices[i].description+")");

              //print out its datalink name and description
              System.out.println(" datalink: "+devices[i].datalink_name + "(" + devices[i].datalink_description+")");

              //print out its MAC address
              System.out.print(" MAC address:");
              for (byte b : devices[i].mac_address)
                System.out.print(Integer.toHexString(b&0xff) + ":");
              System.out.println();

              //print out its IP address, subnet mask and broadcast address
              for (NetworkInterfaceAddress a : devices[i].addresses)
                System.out.println(" address:"+a.address + " " + a.subnet + " "+ a.broadcast);
            }
        //Get the Device information - End

//Capture the packets

                System.out.println("\n \n ");
                System.out.println("Please Enter the Device Name to Capture the Packet");
                Scanner in = new Scanner(System.in);
                int a = in.nextInt();
                if(a <= devices.length)
                {
                int index=a;  // set index of the interface that you want to open.

                //Open an interface with openDevice(NetworkInterface intrface, int snaplen, boolean promics, int to_ms)
                JpcapCaptor captor=JpcapCaptor.openDevice(devices[index], 65535, false, 20);
                captor.setFilter("icmp",true);
                for(int i=0;i<50;i++){
                  //capture a single packet and print it out
                  System.out.println(captor.getPacket());
                  JpcapWriter writer=JpcapWriter.openDumpFile(captor,"s.txt");

                }

                }
                else
                System.out.println("Please Enter the correct value");
            }
}

2 个答案:

答案 0 :(得分:4)

打开设备并设置icmp过滤器后调用looppacket函数: jpcap.loopPacket(-1,new capture());

在捕获类中声明此函数:

public void receivePacket(Packet pkt) {
        IPPacket pac = (IPPacket) pkt;
            System.out.println("Src: " + pac.src_ip + " Dest: " + pac.dst_ip);
    }

答案 1 :(得分:0)

我还没有对它进行过测试,但根据documentation,这应该可以用来获取Source IP address

System.out.println((ICMPPacket)captor.getPacket().src_ip);

一旦获得正确的IP地址,就可以使用此代码轻松获取MAC地址

        InetAddress ip;       
        ip = InetAddress.getLocalHost();
        NetworkInterface network = NetworkInterface.getByInetAddress(ip);
        byte[] mac = network.getHardwareAddress();
        System.out.print("Current MAC address : ");

        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < mac.length; i++) {
          sb.append(String.format("%02X%s", mac[i], (i < mac.length - 1) ? "-" : ""));    
        }
        System.out.println(sb.toString());

感谢mkyong

相关问题