Generating country IP ranges lists

There are many sites providing country blocklists, for example or 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 ="")

for line in lines:
        if (('ipv4' in line) & ('SK' in line)) :
                print "%s/%d" % (net,(32-math.log(cidr,2)))


Alter this script as you need…

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

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

2 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.

    #!/usr/bin/env sh
    # change accordingly

    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)) }';

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

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>