Advertisement
ScratchMonkey

ipdeny-to-NonUS.py

Jan 30th, 2019
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.24 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. # download the current ipdeny list of netblocks assigned to countries
  4. # and dump it into a file, excluding the netblocks for the US.  The
  5. # result can be imported into firewalld ipsets using
  6. # --add-entries-from-file
  7.  
  8. # See http://www.ipdeny.com/ipblocks/
  9.  
  10. # TODO:
  11. # Add command line switch to download ipv6 file.
  12.  
  13. # allow function-like print when using Python 2
  14. from __future__ import print_function
  15.  
  16. import requests
  17. import io
  18. import tarfile
  19. import re
  20.  
  21. ipv4_uri = 'http://www.ipdeny.com/ipblocks/data/countries/all-zones.tar.gz'
  22. ipv6_uri = 'http://www.ipdeny.com/ipv6/ipaddresses/blocks/ipv6-all-zones.tar.gz'
  23.  
  24. # fetch the tarball
  25. r = requests.get(ipv4_uri)
  26. # convert the tarball into a file-like object
  27. f = io.BytesIO(r.content)
  28. # load it into a TarFile
  29. tar = tarfile.open(fileobj=f)
  30. # parse it into a dict of dicts
  31. for member in tar.getmembers():
  32.     # get member content
  33.     # grab the country code
  34.     m = re.search('([a-z][a-z])?.zone', member.name)
  35.     # only consider zone files
  36.     if not m:
  37.         continue
  38.     cc = m.group(1)
  39.     # skip the US one
  40.     if 'us' == cc:
  41.         continue
  42.     print('# ', cc)
  43.     fileobj = tar.extractfile(member)
  44.     netblocks = fileobj.read()
  45.     print(netblocks)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement