IP包流量分析资源下载及源代码、jpcap包的配置(java)

IP包流量分析程序

编制程序,监控网络,捕获一段时间内网络上的IP数据包,按IP数据包的源地址和目的地址统计出该源地址在该时间段内发出的IP包的个数,将其统计结果在控制台现实出来。

程序的具体要求如下:

  1. 获取网络设备列表。

  2. 选择网卡并打开。

  3. 当捕获数据包时,可能需要设置过滤器。

  4. 捕获数据包或者发送数据包。

  5. 统计数据的流量。

首先要进行jpcap包的安装才可以运行以下代码
jpcap的环境配置教程:http://blog.csdn.net/zhangjianmin317/article/details/7376364
64位jpcap包下载地址:http://pan.baidu.com/s/1hr939mW
32位jpcap包下载地址:http://pan.baidu.com/s/1miK1dK8
配置好jpcap后,即可编译运行TestJpcap.java文件

源代码(一共两个java文件,注意)

程序名:TestJpcap.java

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

import jpcap.JpcapCaptor;
import jpcap.NetworkInterface;
import jpcap.NetworkInterfaceAddress;
import jpcap.packet.Packet;

public class TestJpcap {
    public static void main(String[] args) throws IOException {
           Scanner console = new Scanner(System.in);
getDevices();//显示PC机上可用的网卡的信息       
System.out.println("请输入网卡号<0-2>");
int num = console.nextInt();//获取用户要打开的网卡号
System.out.println("请输入抓包时间(分钟)");
int minuets = console.nextInt();//获取用户抓包时间
oneByOneReceiver(num, minuets);//调用抓包方法

    }

    // 获取网络接口列表
    public static void getDevices() {
        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 + ")");
            System.out.print(" MAC address:");
            for (byte b : devices[i].mac_address)
                System.out.print(Integer.toHexString(b & 0xff) + ":");
            System.out.println();
            for (NetworkInterfaceAddress a : devices[i].addresses)
                System.out.println(" address:" + a.address + " " + a.subnet
                        + " " + a.broadcast);
        }
    }

    // 使用逐个捕获方法, 从网络接口捕获数据包
    public static void oneByOneReceiver(int index, int time) throws IOException {
        NetworkInterface[] devices = JpcapCaptor.getDeviceList();
        JpcapCaptor captor = JpcapCaptor.openDevice(devices[index], 65535,
                false, 20);
        PrintWriter pw = new PrintWriter(new FileWriter(new File("packets.txt")));
        // 设置过滤器
        captor.setFilter("ip", true);
        int counter = 0;
        Packet packet;
        long startTime = System.currentTimeMillis();
        while (startTime + time * 60 * 10 >= System.currentTimeMillis()) {
            packet = captor.getPacket();
            System.out.println(packet);
            if (packet != null) {
                String ips = packet.toString().split("\\s+")[1];
                pw.write(ips);
                pw.println();
                counter++;
            }
        }
        pw.close();
        CounterPackets cp = new CounterPackets();
        cp.readPackets();
        cp.print();
        System.out.println("PacketNumbers:" + counter);
    }
}

程序名: CounterPackets.java

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

public class CounterPackets {
    ArrayList ips;//存放读取文本信息
    HashMap counter;//存放地址和对应的数据包的数量
    
    public CounterPackets() {
        counter = new HashMap();
    }

    //从packet.txt.中读取捕获到的IP数据包信息
    public void readPackets() throws IOException {
        ips = new ArrayList();
BufferedReader br = new BufferedReader(new FileReader("packets.txt"));
        while (br.read() != -1) {
            ips.add(br.readLine());
        }   
        counter();
    }

    //统计流量
    public void counter() {
        for (int i = 0; i < ips.size(); i++) {
            String[] result = getIps(ips.get(i));
            String index = result[0] + " " + result[1];
            setNumber(index);
        }
    }
    //获取源地址和目的地址
    public String[] getIps (String ips) {
        StringBuffer sb = new StringBuffer(ips);
        for (int i = 0; i < ips.length(); i++) {
            if (ips.charAt(i) == '/' || ips.charAt(i) == '>' || ips.charAt(i) == '-') {
                sb.replace(i, i + 1, " ");
            }
        }
        ips = sb.toString().trim();
        return ips.split("\\s+");
    }

    //数目叠加
    public void setNumber(String index) {
        if (counter.containsKey(index)) {
            int value = counter.get(index);
            counter.put(index,  value+1);
        } else {
            counter.put(index, 1);
        }
    }
    //将结果打印到控制台
    public void print() {
    System.out.println("源地址" + "       " + "目的地址" + "     " + PacketNumber);
        Iterator it = counter.keySet().iterator();
        while (it.hasNext()) {
            String index = (String)it.next();
            String[] ips = index.split("\\s+");
            String srcIp = String.format("%-6s", ips[0]);
            String dstIp = ips[1];
            int number = counter.get(index);
            System.out.println(srcIp + "        " + dstIp + "          " + number);
        }
    }
}

运行后输入

  1. 首先输入你的网卡号(输入数字(0-2))
    在这我输入1(1是无线网卡号)

  2. 输入抓包时间(就是在一段时间内你访问了哪些目的地址)
    这里我输入2
    然后就可以得到相应的数据(这里我就不上图了)

参考连接

https://wenku.baidu.com/view/02da11b3f605cc1755270722192e453610665b06.html

你可能感兴趣的:(IP包流量分析资源下载及源代码、jpcap包的配置(java))