Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python
- # download the current ipdeny list of netblocks assigned to countries
- # and dump it into a file, excluding the netblocks for the US. The
- # result can be imported into firewalld ipsets using
- # --add-entries-from-file
- # See http://www.ipdeny.com/ipblocks/
- # TODO:
- # Add command line switch to download ipv6 file.
- # allow function-like print when using Python 2
- from __future__ import print_function
- import requests
- import io
- import tarfile
- import re
- ipv4_uri = 'http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz'
- ipv6_uri = 'http://www.ipdeny.com/ipv6/ipaddresses/blocks/ipv6-all-zones.tar.gz'
- # fetch the tarball
- r = requests.get(ipv4_uri)
- # convert the tarball into a file-like object
- f = io.BytesIO(r.content)
- # load it into a TarFile
- tar = tarfile.open(fileobj=f)
- # parse it into a dict of dicts
- for member in tar.getmembers():
- # get member content
- # grab the country code
- m = re.search('([a-z][a-z])?.zone', member.name)
- # only consider zone files
- if not m:
- continue
- cc = m.group(1)
- # skip the US one
- if 'us' == cc:
- continue
- print('# ', cc)
- fileobj = tar.extractfile(member)
- netblocks = fileobj.read()
- print(netblocks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement