Skip to content

Generating country IP ranges lists

There are many sites providing country blocklists, for example http://www.ip2location.com/free/visitor-blocker or https://www.countryipblocks.net/. When you want periodicaly update such list, you have problem. You need pay for that service for automated access, actual data or “hack” access to that service (abuse their free access conditions)…

Is there some easy solution how to generate this ranges by yourself?

Small python script can help to get and parse ranges from RIPE

import math
import urllib
opener = urllib.FancyURLopener()
f = opener.open("ftp://ftp.ripe.net/pub/stats/ripencc/delegated-ripencc-latest")
lines=f.readlines()

for line in lines:
        if (('ipv4' in line) & ('SK' in line)) :
                s=line.split("|")
                net=s[3]
                cidr=float(s[4])
                print "%s/%d" % (net,(32-math.log(cidr,2)))

 

Alter this script as you need…

For other regions use proper Regional Internet Registries (RIRs):

ftp://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest
ftp://ftp.ripe.net/ripe/stats/delegated-ripencc-latest
ftp://ftp.afrinic.net/pub/stats/afrinic/delegated-afrinic-latest
ftp://ftp.apnic.net/pub/stats/apnic/delegated-apnic-latest
ftp://ftp.lacnic.net/pub/stats/lacnic/delegated-lacnic-latest

UPDATE:  See my next post about country lists download from my generator.
UPDATE #2: Similar script in php using different source in this post.

8 thoughts on “Generating country IP ranges lists”

  1. I had no idea that the country/IP delegations were readily available. This is exactly what I needed. However, I wanted it to run on an OpenWRT installation and I didn’t want to install python, so I wrote a shell/curl/awk version.

    [code]
    #!/usr/bin/env sh
    # change accordingly
    country_code=’SK’;
    delegations_url=’http://ftp.arin.net/pub/stats/arin/delegated-arin-extended-latest’

    curl $delegations_url –silent | awk -v country_code=$country_code -F ‘|’ ‘BEGIN { pattern=country_code”\\|ipv4″; } $0 ~ pattern {print $4 “/” (32-log($5)/log(2)) }’;
    [/code]

  2. Pingback: 8 Ways to Block Visitors to Your Website by Country • Raymond.CC

  3. Fuckin awesome.

    The valuable part of this is the knowledge that it can be scooped off of the registrars FTPs. Thank you for learning me this.

Leave a Reply

Your email address will not be published. Required fields are marked *