Skip to content

Instantly share code, notes, and snippets.

@bazooka07
Created February 18, 2018 22:48
Show Gist options
  • Save bazooka07/5e743e2572fec54b6e1f81f60263da0a to your computer and use it in GitHub Desktop.
Save bazooka07/5e743e2572fec54b6e1f81f60263da0a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# pip3 install IP2Location
# (8.0.0) - Python API for IP2Location database
# https://www.ip2location.com/databases/db3-ip-country-region-city
import IP2Location
import ipaddress
# 178.64.0.0/24
# 171.33.248.0/21
ips = [
'46.29.20.203',
'178.159.37.18',
'178.69.38.250',
'178.93.155.198',
'91.200.12.106',
'91.200.12.63',
'91.200.12.233',
'178.120.76.190',
'185.220.101.42',
'185.36.102.114',
'195.211.213.1',
'5.164.251.58',
'93.170.187.48',
'93.188.39.169'
]
print('''\
# For .htaccess
Order Allow,Deny
Allow From All''')
ip2loc = IP2Location.IP2Location()
ip2loc.open('IP2LOCATION-LITE-DB1.BIN')
for ip in ips:
rec = ip2loc.find(ip)
ip1 =ipaddress.ip_address(rec.ip)
mask = 1
step = 32
while step > 0 and (int(ip1) & mask) == 0:
mask *= 2
step -= 1
print('Deny From %16s/%d #%16s (%s)' % (rec.ip, step, ip, rec.country_long.decode('UTF-8')))
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment