从 APNIC 获取中国大陆地区的 IP 地址数据库

全球互联网 IP 地址分配由互联网号码分配局(IANA)机构负责。IANA 将分配工作委托给五大区域互联网机构管理(RIR),其中亚太网络信息中心(APNIC)负责管理亚太地区的事务。我们可以在 APNIC 的网站上找到所有分配给亚太地区的 IPv4/IPv6 地址数据库。通过该数据库,可以准确地知道某个 IP 地被分配给了哪个国家或地区。

打开 APNIC 网站首页,在导航菜单中选择「Insights -> Raw Data」,进入 ftp 数据下载站点。也可以通过 http 接口访问 ftp 目录。下载最新数据文件:delegated-apnic-latest

数据文件是竖线分隔的 CSV 文件,其格式如下:

registry|cc|type|start|value|date|status

各字段的含义如下:

registry    The registry from which the data is taken.
            For APNIC resources, this will be:
                apnic

cc          ISO 3166 2-letter code of the organisation to
            which the allocation or assignment was made. 

type        Type of Internet number resource represented
            in this record. One value from the set of 
            defined strings:
                {asn,ipv4,ipv6}

start       In the case of records of type 'ipv4' or
            'ipv6' this is the IPv4 or IPv6 'first
            address' of the	range.
            In the case of an 16 bit AS number, the
            format is the integer value in the range:
                0 - 65535
            In the case of a 32 bit ASN,  the value is
            in the range:
                0 - 4294967296
            No distinction is drawn between 16 and 32
            bit ASN values in the range 0 to 65535.

value       In the case of IPv4 address the count of
            hosts for this range. This count does not 
            have to represent a CIDR range.
            In the case of an IPv6 address the value 
            will be the CIDR prefix length from the 
            'first address'	value of <start>.
            In the case of records of type 'asn' the 
            number is the count of AS from this start 
            value.

date        Date on this allocation/assignment was made
            by the RIR in the format:
                YYYYMMDD
            Where the allocation or assignment has been
            transferred from another registry, this date
            represents the date of first assignment or
            allocation as received in from the original
            RIR.
            It is noted that where records do not show a 
            date of	first assignment, this can take the 
            0000/00/00 value.

status      Type of allocation from the set:
                {allocated, assigned}
            This is the allocation or assignment made by 
            the registry producing the file and not any
            sub-assignment by other agencies.

其中 cc 字段就是代表国家或地区的代码,若此字段为 CN,则代表中国大陆。此外,HK 代表香港,TW 代表台湾,MO 代表澳门。