Created
May 5, 2026 19:57
-
-
Save rennerocha/5fdb5b911d7b50259cbf4b91dd224baa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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