需求
找出属于特定国家的所有IP段,例如中国
apnic: 亚太互联网络信息中心 (Asia-Pacific Network Information Center,APNIC),是全球五大区域性因特网注册管理机构之一,负责亚太地区IP地址、ASN(自治域系统号)的分配并管理一部分根域名服务器镜像的国际组织。(from baidu)
下载并过滤
IP地址分配情况列表:https://ftp.apnic.net/stats/apnic/delegated-apnic-latest
脚本get-cn-ipv4-network.sh,Kimi生成
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| #!/bin/bash
TODAY=$(date +%Y%m%d)
URL="https://ftp.apnic.net/stats/apnic/delegated-apnic-latest"
OUTPUT_FILE="delegated-apnic-cn-ipv4-${TODAY}.txt"
if [ ! -f delegated-apnic-latest-${TODAY}.txt ]; then wget -q -O delegated-apnic-latest-${TODAY}.txt "$URL" fi
declare -A mask_length_map for i in {0..32}; do mask_length_map[$((1<<$i))]=$((32-$i)) done
grep -E "CN\|ipv4" delegated-apnic-latest-${TODAY}.txt | while read line; do PREFIX=$(echo "$line" | awk -F'|' '{print $4}') HOSTS=$(echo "$line" | awk -F'|' '{print $5}')
MASK_LENGTH=${mask_length_map[$HOSTS]}
echo "${PREFIX}/${MASK_LENGTH}" >> "$OUTPUT_FILE" done
echo "Output file: ${OUTPUT_FILE}"
|
当然也可以计算下总的IP地址数量,可以看出咱们中国ipv4地址占比很低
1 2 3 4 5 6 7 8 9
| localhost ~ # awk -F'|' '/CN\|ipv4/ {sum += $5} END {print sum}' delegated-apnic-latest-20241024.txt 343144192 localhost ~ # echo $((1<<32)) 4294967296 localhost ~ # python Python 3.12.3 (main, Aug 27 2024, 16:40:28) [GCC 13.3.1 20240614] on linux Type "help", "copyright", "credits" or "license" for more information. >>> 343144192/4294967296.0 0.07989448308944702
|