improve whois

This commit is contained in:
Babak Farrokhi 2016-05-04 12:09:13 +04:30
parent 380c357e5f
commit f167739ea2
Signed by: farrokhi
GPG Key ID: 6B267AD85D632E9A
2 changed files with 32 additions and 43 deletions

View File

@ -29,6 +29,7 @@ import concurrent.futures
import getopt import getopt
import ipaddress import ipaddress
import os import os
import pickle
import signal import signal
import socket import socket
import sys import sys
@ -36,21 +37,17 @@ import time
import dns.rdatatype import dns.rdatatype
import dns.resolver import dns.resolver
from cymruwhois import cymruwhois
__author__ = 'Babak Farrokhi (babak@farrokhi.net)' __author__ = 'Babak Farrokhi (babak@farrokhi.net)'
__license__ = 'BSD' __license__ = 'BSD'
__version__ = 1.3 __version__ = 1.3
def test_import(): def test_import():
# passing this test means imports were successful
pass pass
# http://pythonhosted.org/cymruwhois/
try:
import cymruwhois
has_whois = True
except ImportError:
has_whois = False
# Constants # Constants
__PROGNAME__ = os.path.basename(sys.argv[0]) __PROGNAME__ = os.path.basename(sys.argv[0])
@ -76,36 +73,32 @@ class Colors(object):
# Globarl Variables # Globarl Variables
should_stop = False should_stop = False
if has_whois:
from cymruwhois import cymruwhois
import pickle
def whoisrecord(ip):
try:
currenttime = time.time()
ts = currenttime
if ip in whois:
ASN, ts = whois[ip]
else:
ts = 0
if ((currenttime - ts) > 36000):
c = cymruwhois.Client()
ASN = c.lookup(ip)
whois[ip] = (ASN, currenttime)
return ASN
except Exception as e:
return e
def whoisrecord(ip):
try: try:
pkl_file = open(WHOIS_CACHE, 'rb') currenttime = time.time()
try: ts = currenttime
whois = pickle.load(pkl_file) if ip in whois:
except EOFError: ASN, ts = whois[ip]
whois = {} else:
except IOError: ts = 0
if ((currenttime - ts) > 36000):
c = cymruwhois.Client()
ASN = c.lookup(ip)
whois[ip] = (ASN, currenttime)
return ASN
except Exception as e:
return e
try:
pkl_file = open(WHOIS_CACHE, 'rb')
try:
whois = pickle.load(pkl_file)
except EOFError:
whois = {} whois = {}
except IOError:
whois = {}
def usage(): def usage():
@ -255,10 +248,7 @@ def main():
elif o in ("-n"): elif o in ("-n"):
should_resolve = False should_resolve = False
elif o in ("-a", "--asn"): elif o in ("-a", "--asn"):
if has_whois: as_lookup = True
as_lookup = True
else:
print('Warning: cymruwhois module cannot be loaded. AS Lookup disabled.')
else: else:
usage() usage()
@ -339,7 +329,7 @@ def main():
if curr_addr: if curr_addr:
as_name = "" as_name = ""
if has_whois and as_lookup: if as_lookup:
ASN = whoisrecord(curr_addr) ASN = whoisrecord(curr_addr)
as_name = '' as_name = ''
try: try:
@ -379,6 +369,5 @@ if __name__ == '__main__':
try: try:
main() main()
finally: finally:
if has_whois: pkl_file = open(WHOIS_CACHE, 'wb')
pkl_file = open(WHOIS_CACHE, 'wb') pickle.dump(whois, pkl_file)
pickle.dump(whois, pkl_file)

View File

@ -1,7 +1,7 @@
from setuptools import setup, find_packages from setuptools import setup, find_packages
setup( setup(
name = "dnsdiag", name = "dnsdiag",
version = "1.3.1", version = "1.3.2",
packages = find_packages(), packages = find_packages(),
scripts = ['dnsping.py', 'dnstraceroute.py', 'dnseval.py'], scripts = ['dnsping.py', 'dnstraceroute.py', 'dnseval.py'],
classifiers=[ classifiers=[