问题
Does anyone know how to look up an IP4 address from MaxMind's GeoLite2 Country CSV using SQL?
I have been using MaxMind's free GeoIP data for many years, and would like to upgrade to their GeoLite2 data. I have the blocks and locations data loaded into MySQL tables, but am not sure how to determine the address range that an IP4 address falls into. The old format had a start/end number for each block; the new format only seems to have a start number.
I have already hunted through the MaxMind developer docs, and Googled, but can't seem to find any information on how to query the new format. I'm sure it's obvious, and will edit this posting if I figure it out in the interim.
I think thank I'd have to find the first blocks entry that is greater or equal to the IP4 address, and LIMIT 1, perhaps.
I use this data both for web application looks, and for querying directly in SQL for generating reports; so I usually need to make sure I can implement the lookup in both Perl code and pure SQL.
I am upgrading because I'm seeing some funny results for Japanese visitors appearing to be from France on the old data.
many thanks
回答1:
The address format used in Geolite2 CSV includes a block IP address start, followed by a Prefix length #, which can be converted into the block IP address end.
(Somewhat confusingly, Maxmind is using "Network_Mask_Length" instead of "Prefix_Length", the accepted IPv6 terminology, to label this field.)
Geolite2 CSV's blocks fields layout:
network_start_ip,network_mask_length,geoname_id,registered_country_geoname_id,re
presented_country_geoname_id,postal_code,latitude,longitude,is_anonymous_proxy,i
s_satellite_provider
Exemple:(record extracted from Geolite2-Country-Blocks.csv)
::ffff:81.248.136.0,120,3578476,3017382,,,,,0,0
Given the above exemple, what is the Last IPv4 address assigned to the block?
First IP address: 81.248.136.0
Prefix_Length/Network_mask_Length: 120
Last IP address: 81.248.136.255
The following URL might be handy to quickly look up the number of IP addresses available for a specific Prefix_Length:
http://www.gestioip.net/cgi-bin/subnet_calculator.cgi
__philippe
回答2:
Prefix_Length calculator usage:
(In this case, more of a simple Table Lookup tool than a Calculator, really...;-)
http://www.gestioip.net/cgi-bin/subnet_calculator.cgi
In the calculator, tick the IPv6 button, then click the PL box down arrow. A list of "Prefix Length" will be presented, with the corresponding available number of IP addresses.
To determine the last IP adress of any Geolite2 block, the following relevant range of Prefix_Length/address pairs should most likely suffice:
Prefix #addresses
Length
117 2048
118 1024
119 512
120 256
121 128
122 64
123 32
124 16
125 8
126 4
127 2
128 1
Note that the Geolite2 file structure follows a form of hybrid IPv4/IPv6 notation, aka "IPv4-mapped-IPv6 address".
Those "hybrid" addresses are written with the first 96 bits in the standard IPv6 format, and the remaining 32 bits written in the customary dot-decimal notation of IPv4. For instance, ::ffff:192.0.2.128 represents the IPv4 address 192.0.2.128
For much more on this very(hairy) subject, check here:
http://en.wikipedia.org/wiki/IPv6
__philippe
来源:https://stackoverflow.com/questions/22002542/querying-geolite2-country-csv-in-sql