Created
August 28, 2024 13:28
-
-
Save flodolo/a13c131aa83d1f974fb12a72cab55bc5 to your computer and use it in GitHub Desktop.
Check Heroku log for IPs
This file contains 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 | |
import sys | |
if len(sys.argv) == 1: | |
sys.exit("Provide the log file as an argument") | |
ips = {} | |
filter = re.compile(r"fwd=\"([\d.]+)\"") | |
threshold = 10 | |
# Copy from Heroku settings | |
blocked_ips="" | |
with open(sys.argv[1]) as f: | |
content = f.readlines() | |
for line in content: | |
match = filter.search(line) | |
if match: | |
ip = match.group(1) | |
if ip not in ips: | |
ips[ip] = 1 | |
else: | |
ips[ip] += 1 | |
sorted_ips = dict(sorted(ips.items(), key=lambda item: item[1], reverse=True)) | |
blocked_ips = blocked_ips.split(",") | |
print(f"Ignoring IPs listed less than {threshold} times or already blocked.") | |
for ip, count in sorted_ips.items(): | |
if count < threshold or ip in blocked_ips: | |
continue | |
print(f"{ip}: {count}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment