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 正能量
*
*/
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));
}
}补充阅读#
解释通过 APNIC 委派数据解析中国 IPv4 地址段的用途,以及地区登记信息的边界。
如何理解与使用#
原题“Apanic”指 APNIC。脚本处理的是地址资源登记数据,读取记录时需要区分地区字段、资源类型、起始地址和数量。
验证与边界#
登记地区不等于实时用户所在地,也不能单独用于精确定位。验证时覆盖地址段边界、数据更新时间与非 IPv4 记录,避免简单按字符串拆分后误算。
