当前位置:正能量 » 代码编程 » Java » 通过Apanic解析中国IP

通过Apanic解析中国IP

package com.ygsoft.bxj.api;

import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.util.List;

import org.apache.commons.io.FileUtils;

import com.google.common.collect.Lists;

/**
 * 
 * @author 吴云辉 fly@mmgg.net
 *
 */
public class Test {

    public static void main(String[] args) throws IOException {
        getFile();
        List<String> list = FileUtils.readLines(new File("e:/aa.txt"));
        List<String> ips = Lists.newArrayList();
        for (String string : list) {
            if (string.matches("^apnic\\|CN\\|ipv4\\|.+")) {
                String[] str = string.split("\\|");
                ips.add(str[3] + "/" + toMask(str[4]));
            }
        }
        for (String string : ips) {
            System.out.println("/ip firewall address-list add list=chinaips address=" + string);
        }
    }

    private static void getFile() throws IOException {
        URL url = new URL("http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest");
        FileUtils.copyURLToFile(url, new File("e:/aa.txt"));
    }

    private static int toMask(String rang) {
        int r = Integer.parseInt(rang);
        return (int) (32 - Math.log(r) / Math.log(2));
    }

}
未经允许不得转载:正能量 » 通过Apanic解析中国IP

相关文章

评论 (0)

8 + 4 =