Skip to content

Instantly share code, notes, and snippets.

@rennerocha
Created May 5, 2026 19:57
Show Gist options
  • Select an option

  • Save rennerocha/5fdb5b911d7b50259cbf4b91dd224baa to your computer and use it in GitHub Desktop.

Select an option

Save rennerocha/5fdb5b911d7b50259cbf4b91dd224baa to your computer and use it in GitHub Desktop.
import re
IP_REGEX = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}) "
CIDR_REGEX = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\/\d{1,3})"
BASE_IP_REGEX = r"(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})"
unique_ips = {}
with open("ip-blocklist.conf.short", "r") as blocklist:
for row in blocklist:
is_cidr = False
if re_m := re.search(CIDR_REGEX, row):
value = re_m.group().strip()
base_ip_m = re.search(BASE_IP_REGEX, row)
base_ip = base_ip_m.group()
is_cidr = True
elif re_m := re.search(IP_REGEX, row):
value = re_m.group().strip()
base_ip = value
is_cidr = False
else:
continue
if base_ip not in unique_ips:
unique_ips[base_ip] = value
else:
if is_cidr:
# Replace because we prefer CIDR
unique_ips[base_ip] = value
for ip in unique_ips.values():
print(ip)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment