Skip to content

Instantly share code, notes, and snippets.

@nook-ru
Last active November 28, 2018 12:57
Show Gist options
  • Save nook-ru/531e10d4eab8033d542aa5b0d6f76682 to your computer and use it in GitHub Desktop.
Save nook-ru/531e10d4eab8033d542aa5b0d6f76682 to your computer and use it in GitHub Desktop.
#!/usr/bin/env fail2ban-python
import sys
def process_args(argv):
if len(argv) != 2:
sys.stderr.write("Please provide a single IP as an argument. Got: %s\n"
% (argv[1:]))
sys.exit(2)
ip = argv[1]
from fail2ban.server.filter import DNSUtils
if not DNSUtils.isValidIP(ip):
sys.stderr.write("Argument must be a single valid IP. Got: %s\n"
% ip)
sys.exit(3)
return ip
def is_yandexbot(ip):
import re
from fail2ban.server.filter import DNSUtils
host = DNSUtils.ipToName(ip)
if not host or not re.match('.*\.yandex\.(com|ru|net)$', host):
sys.exit(1)
host_ips = DNSUtils.dnsToIp(host)
sys.exit(0 if ip in host_ips else 1)
if __name__ == '__main__':
is_yandexbot(process_args(sys.argv))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment