-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
reverse_dns.py
69 lines (50 loc) · 1.84 KB
/
reverse_dns.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
"""ReverseDNS, Perform a reverse dns lookup on fields in the ip_field_list."""
from __future__ import print_function
import socket
# Local imports
from zat.utils import net_utils, cache
class ReverseDNS(object):
"""Perform a reverse dns lookup on fields in the ip_field_list."""
def __init__(self, lookup_internal=False):
"""Initialize ReverseDNS Class"""
self.ip_lookup_cache = cache.Cache(timeout=600)
self.lookup_internal = lookup_internal
def lookup(self, ip_address):
"""Try to do a reverse dns lookup on the given ip_address"""
# Is this already in our cache
if self.ip_lookup_cache.get(ip_address):
domain = self.ip_lookup_cache.get(ip_address)
# Is the ip_address local or special
elif not self.lookup_internal and net_utils.is_internal(ip_address):
domain = 'internal'
elif net_utils.is_special(ip_address):
domain = net_utils.is_special(ip_address)
# Look it up at this point
else:
domain = self._reverse_dns_lookup(ip_address)
# Cache it
self.ip_lookup_cache.set(ip_address, domain)
# Return the domain
return domain
@staticmethod
def _reverse_dns_lookup(ip_address):
"""Perform the reverse DNS lookup
Args:
ip_address: the ip_address (as a str)
Returns:
the domain given by a reverse DNS request on the ip address
"""
# Look it up
try:
return socket.gethostbyaddr(ip_address)[0]
except socket.herror:
return 'nxdomain'
def test():
"""Test for ReverseDNS class"""
dns = ReverseDNS()
print(dns.lookup('192.168.0.1'))
print(dns.lookup('8.8.8.8'))
# Test cache
print(dns.lookup('8.8.8.8'))
if __name__ == '__main__':
test()