-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCachedDNS.py
More file actions
executable file
·61 lines (42 loc) · 1.68 KB
/
Copy pathCachedDNS.py
File metadata and controls
executable file
·61 lines (42 loc) · 1.68 KB
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
#!/usr/bin/env python
"""DNS lookup with cache"""
import socket
class LookupIP(object):
"""
Map IP's to hostnames using local cache where possible, using DNS lookups
otherwise
"""
def __init__(self):
self.__def_hostname = "-unknown-"
self.__hostname_cache = dict()
self.__hostname_cache["0.0.0.0"] = self.__def_hostname
def get_cached_hostname(self, ip_addr):
"""Return the hostname associated with the given IP address."""
if ip_addr in self.__hostname_cache:
__ip2host = self.__hostname_cache[ip_addr]
else:
try:
(__ip2host, __ip2alias, __ip2iplist) = socket.gethostbyaddr(
ip_addr)
except (socket.error, socket.herror, socket.timeout):
__ip2host = ""
if __ip2host == "":
__ip2host = self.__def_hostname
self.__hostname_cache[ip_addr] = __ip2host
return __ip2host
def get_cache_entry(self, ip_addr):
"""Return the cache entry for the given IP address."""
if ip_addr in self.__hostname_cache:
__ip2host = self.__hostname_cache[ip_addr]
else:
__ip2host = self.__def_hostname
return __ip2host
def flush_cache(self):
"""Discard the current cache of names/IP's"""
self.__hostname_cache = dict()
self.__hostname_cache["0.0.0.0"] = self.__def_hostname
def get_cache_list(self):
"""Return a copy of the cached names/IP's"""
return self.__hostname_cache
if __name__ == "__main__":
print "This is a collection of routines to perform DNS lookups"