Last active
July 13, 2023 19:18
-
-
Save Strykar/f190bcc36778e1119d70b15eace0bafe to your computer and use it in GitHub Desktop.
Script to work with Fail2ban ignorecommand - https://manpages.debian.org/experimental/fail2ban/jail.conf.5.en.html#ignorecommand
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
#!/usr/bin/env python3 | |
import sys | |
import dns.resolver | |
def resolve_ips(domain, resolver): | |
ips = [] | |
try: | |
ips += [str(rdata) for rdata in resolver.resolve(domain, 'A')] | |
except (dns.resolver.NoAnswer, dns.resolver.Timeout): | |
pass | |
try: | |
ips += [str(rdata) for rdata in resolver.resolve(domain, 'AAAA')] | |
except (dns.resolver.NoAnswer, dns.resolver.Timeout): | |
pass | |
return ips | |
# Resolving hostnames to IP addresses using 8.8.8.8 DNS server | |
resolver1 = dns.resolver.Resolver() | |
resolver1.nameservers = ['8.8.8.8'] | |
resolver1.timeout = 27.5 | |
resolver1.lifetime = 27.5 | |
INGEST_IPS = resolve_ips('ingest.multiorch.com', resolver1) | |
V1_IPS = resolve_ips('i5.4v1.in', resolver1) | |
# If resolution was unsuccessful, try with 1.1.1.1 DNS server | |
if not INGEST_IPS or not V1_IPS: | |
resolver2 = dns.resolver.Resolver() | |
resolver2.nameservers = ['1.1.1.1'] | |
resolver2.timeout = 27.5 | |
resolver2.lifetime = 27.5 | |
if not INGEST_IPS: | |
INGEST_IPS = resolve_ips('ingest.multiorch.com', resolver2) | |
if not V1_IPS: | |
V1_IPS = resolve_ips('i5.4v1.in', resolver2) | |
# Check if IP is from ingest.multiorch.com or i5.4v1.in | |
if sys.argv[1] in INGEST_IPS + V1_IPS: | |
sys.exit(0) | |
# If IP was not from the allowed hosts, return 1 | |
sys.exit(1) |
Author
Strykar
commented
Jul 13, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment