Subversion Repositories configs

Rev

Rev 71 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
87 - 1
#!/usr/bin/env fail2ban-python
34 - 2
# Inspired by https://isc.sans.edu/forums/diary/When+Google+isnt+Google/15968/
3
#
4
# Written in Python to reuse built-in Python batteries and not depend on
5
# presence of host and cut commands
6
#
7
import sys
8
 
9
def process_args(argv):
10
    if len(argv) != 2:
11
       sys.stderr.write("Please provide a single IP as an argument. Got: %s\n"
12
                        % (argv[1:]))
13
       sys.exit(2)
14
 
15
    ip = argv[1]
16
 
17
    from fail2ban.server.filter import DNSUtils
18
    if not DNSUtils.isValidIP(ip):
19
       sys.stderr.write("Argument must be a single valid IP. Got: %s\n"
20
                        % ip)
21
       sys.exit(3)
22
    return ip
23
 
24
def is_googlebot(ip):
25
    import re
26
    from fail2ban.server.filter import DNSUtils
27
 
28
    host = DNSUtils.ipToName(ip)
71 - 29
    if not host or not re.match('.*\.google(bot)?\.com$', host):
30
       sys.exit(1)
31
    host_ips = DNSUtils.dnsToIp(host)
32
    sys.exit(0 if ip in host_ips else 1)
34 - 33
 
34
if __name__ == '__main__':
35
    is_googlebot(process_args(sys.argv))