Compare commits
8 Commits
Author | SHA1 | Date | |
---|---|---|---|
47dbc6afe3
|
|||
39e564e626
|
|||
3df692e6db
|
|||
2dcf0e7b78
|
|||
a0f9cae673
|
|||
51e3d252f2
|
|||
7db7684c95 | |||
bde3263cfa
|
3
.gitmodules
vendored
3
.gitmodules
vendored
@ -1,3 +0,0 @@
|
||||
[submodule "cymruwhois"]
|
||||
path = cymruwhois
|
||||
url = https://github.com/JustinAzoff/python-cymruwhois.git
|
||||
|
@ -1,2 +1 @@
|
||||
include LICENSE README.md TODO.md public-servers.txt
|
||||
include cymruwhois/*.py
|
||||
|
@ -29,8 +29,6 @@ of view.
|
||||
This script requires python3 as well as latest
|
||||
[dnspython](http://www.dnspython.org/) and
|
||||
[cymruwhois](https://pythonhosted.org/cymruwhois/).
|
||||
All required third-party modules are included as GIT submodules. You just need
|
||||
to run `git submodule update --init` and project directory to pull the required code.
|
||||
|
||||
# installation
|
||||
|
||||
@ -43,13 +41,13 @@ There are several ways that you can use this toolset. However using the sourceco
|
||||
```
|
||||
git clone https://github.com/farrokhi/dnsdiag.git
|
||||
cd dnsdiag
|
||||
git submodule update --init
|
||||
pip3 install -r requirements.txt
|
||||
```
|
||||
|
||||
2. You can alternatively install the package using pip:
|
||||
|
||||
```
|
||||
pip3 install --process-dependency-links dnsdiag
|
||||
pip3 install dnsdiag
|
||||
```
|
||||
|
||||
## From Binary
|
||||
|
Submodule cymruwhois deleted from a34543335c
48
dnseval.py
48
dnseval.py
@ -38,15 +38,33 @@ from statistics import stdev
|
||||
import dns.rdatatype
|
||||
import dns.resolver
|
||||
|
||||
__VERSION__ = 1.6
|
||||
__PROGNAME__ = os.path.basename(sys.argv[0])
|
||||
__author__ = 'Babak Farrokhi (babak@farrokhi.net)'
|
||||
__license__ = 'BSD'
|
||||
__version__ = "1.6.3"
|
||||
__progname__ = os.path.basename(sys.argv[0])
|
||||
shutdown = False
|
||||
|
||||
resolvers = dns.resolver.get_default_resolver().nameservers
|
||||
|
||||
|
||||
class Colors(object):
|
||||
N = '\033[m' # native
|
||||
R = '\033[31m' # red
|
||||
G = '\033[32m' # green
|
||||
O = '\033[33m' # orange
|
||||
B = '\033[34m' # blue
|
||||
|
||||
def __init__(self, mode):
|
||||
if not mode:
|
||||
self.N = ''
|
||||
self.R = ''
|
||||
self.G = ''
|
||||
self.O = ''
|
||||
self.B = ''
|
||||
|
||||
|
||||
def usage():
|
||||
print("""%s version %1.1f
|
||||
print("""%s version %s
|
||||
|
||||
usage: %s [-h] [-f server-list] [-c count] [-t type] [-w wait] hostname
|
||||
-h --help show this help
|
||||
@ -56,8 +74,9 @@ usage: %s [-h] [-f server-list] [-c count] [-t type] [-w wait] hostname
|
||||
-t --type DNS request record type (default: A)
|
||||
-T --tcp Use TCP instead of UDP
|
||||
-e --edns Disable EDNS0 (Default: Enabled)
|
||||
-C --color Print colorful output
|
||||
-v --verbose Print actual dns response
|
||||
""" % (__PROGNAME__, __VERSION__, __PROGNAME__))
|
||||
""" % (__progname__, __version__, __progname__))
|
||||
sys.exit()
|
||||
|
||||
|
||||
@ -194,11 +213,12 @@ def main():
|
||||
use_tcp = False
|
||||
use_edns = True
|
||||
verbose = False
|
||||
color_mode = False
|
||||
hostname = 'wikipedia.org'
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "hf:c:t:w:Tev",
|
||||
["help", "file=", "count=", "type=", "wait=", "tcp", "edns", "verbose"])
|
||||
opts, args = getopt.getopt(sys.argv[1:], "hf:c:t:w:TevC",
|
||||
["help", "file=", "count=", "type=", "wait=", "tcp", "edns", "verbose", "color"])
|
||||
except getopt.GetoptError as err:
|
||||
print(err)
|
||||
usage()
|
||||
@ -224,12 +244,16 @@ def main():
|
||||
use_tcp = True
|
||||
elif o in ("-e", "--edns"):
|
||||
use_edns = False
|
||||
elif o in ("-C", "--color"):
|
||||
color_mode = True
|
||||
elif o in ("-v", "--verbose"):
|
||||
verbose = True
|
||||
else:
|
||||
print("Invalid option: %s" % o)
|
||||
usage()
|
||||
|
||||
color = Colors(color_mode)
|
||||
|
||||
try:
|
||||
if fromfile:
|
||||
with open(inputfilename, 'rt') as flist:
|
||||
@ -286,12 +310,16 @@ def main():
|
||||
if s_ttl == "None":
|
||||
s_ttl = "N/A"
|
||||
|
||||
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %%%-3d %-8s %21s" % (
|
||||
s, r_avg, r_min, r_max, r_stddev, r_lost_percent, s_ttl, text_flags), flush=True)
|
||||
if verbose:
|
||||
if r_lost_percent > 0:
|
||||
l_color = color.O
|
||||
else:
|
||||
l_color = color.N
|
||||
print("%s %-8.3f %-8.3f %-8.3f %-8.3f %s%%%-3d%s %-8s %21s" % (
|
||||
s, r_avg, r_min, r_max, r_stddev, l_color, r_lost_percent, color.N, s_ttl, text_flags), flush=True)
|
||||
if verbose and hasattr(answers, 'response'):
|
||||
ans_index = 1
|
||||
for answer in answers.response.answer:
|
||||
print("Answer %d [ %s ]" % (ans_index, answer))
|
||||
print("Answer %d [ %s%s%s ]" % (ans_index, color.B, answer, color.N))
|
||||
ans_index += 1
|
||||
print("")
|
||||
|
||||
|
12
dnsping.py
12
dnsping.py
@ -38,13 +38,15 @@ import dns.flags
|
||||
import dns.rdatatype
|
||||
import dns.resolver
|
||||
|
||||
__VERSION__ = 1.6
|
||||
__PROGNAME__ = os.path.basename(sys.argv[0])
|
||||
__author__ = 'Babak Farrokhi (babak@farrokhi.net)'
|
||||
__license__ = 'BSD'
|
||||
__version__ = "1.6.3"
|
||||
__progname__ = os.path.basename(sys.argv[0])
|
||||
shutdown = False
|
||||
|
||||
|
||||
def usage():
|
||||
print("""%s version %1.1f
|
||||
print("""%s version %s
|
||||
usage: %s [-ehqv] [-s server] [-p port] [-P port] [-S address] [-c count] [-t type] [-w wait] hostname
|
||||
-h --help Show this help
|
||||
-q --quiet Quiet
|
||||
@ -61,7 +63,7 @@ usage: %s [-ehqv] [-s server] [-p port] [-P port] [-S address] [-c count] [-t ty
|
||||
-i --interval Time between each request (default: 0 seconds)
|
||||
-t --type DNS request record type (default: A)
|
||||
-e --edns Disable EDNS0 (default: Enabled)
|
||||
""" % (__PROGNAME__, __VERSION__, __PROGNAME__))
|
||||
""" % (__progname__, __version__, __progname__))
|
||||
sys.exit(0)
|
||||
|
||||
|
||||
@ -172,7 +174,7 @@ def main():
|
||||
response_time = []
|
||||
i = 0
|
||||
|
||||
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dst_port, hostname, dnsrecord))
|
||||
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__progname__, dnsserver, dst_port, hostname, dnsrecord))
|
||||
|
||||
for i in range(count):
|
||||
if shutdown:
|
||||
|
@ -39,13 +39,20 @@ import dns.query
|
||||
import dns.rdatatype
|
||||
import dns.resolver
|
||||
|
||||
from cymruwhois import cymruwhois
|
||||
import cymruwhois
|
||||
|
||||
# Global Variables
|
||||
__author__ = 'Babak Farrokhi (babak@farrokhi.net)'
|
||||
__license__ = 'BSD'
|
||||
__version__ = 1.6
|
||||
__version__ = "1.6.3"
|
||||
_ttl = None
|
||||
quiet = False
|
||||
whois_cache = {}
|
||||
shutdown = False
|
||||
|
||||
# Constants
|
||||
__progname__ = os.path.basename(sys.argv[0])
|
||||
WHOIS_CACHE = 'whois.cache'
|
||||
|
||||
|
||||
class CustomSocket(socket.socket):
|
||||
@ -64,11 +71,6 @@ def test_import():
|
||||
pass
|
||||
|
||||
|
||||
# Constants
|
||||
__PROGNAME__ = os.path.basename(sys.argv[0])
|
||||
WHOIS_CACHE = 'whois.cache'
|
||||
|
||||
|
||||
class Colors(object):
|
||||
N = '\033[m' # native
|
||||
R = '\033[31m' # red
|
||||
@ -85,40 +87,46 @@ class Colors(object):
|
||||
self.B = ''
|
||||
|
||||
|
||||
# Globarl Variables
|
||||
shutdown = False
|
||||
|
||||
|
||||
def whoisrecord(ip):
|
||||
def whois_lookup(ip):
|
||||
try:
|
||||
currenttime = time.perf_counter()
|
||||
global whois_cache
|
||||
currenttime = time.time()
|
||||
ts = currenttime
|
||||
if ip in whois:
|
||||
asn, ts = whois[ip]
|
||||
if ip in whois_cache:
|
||||
asn, ts = whois_cache[ip]
|
||||
else:
|
||||
ts = 0
|
||||
if (currenttime - ts) > 36000:
|
||||
c = cymruwhois.Client()
|
||||
asn = c.lookup(ip)
|
||||
whois[ip] = (asn, currenttime)
|
||||
whois_cache[ip] = (asn, currenttime)
|
||||
return asn
|
||||
except Exception as e:
|
||||
return e
|
||||
|
||||
|
||||
try:
|
||||
pkl_file = open(WHOIS_CACHE, 'rb')
|
||||
def load_whois_cache(cachefile):
|
||||
try:
|
||||
whois = pickle.load(pkl_file)
|
||||
except EOFError:
|
||||
pkl_file = open(cachefile, 'rb')
|
||||
try:
|
||||
whois = pickle.load(pkl_file)
|
||||
pkl_file.close()
|
||||
except Exception:
|
||||
whois = {}
|
||||
except IOError:
|
||||
whois = {}
|
||||
except IOError:
|
||||
whois = {}
|
||||
return whois
|
||||
|
||||
|
||||
def save_whois_cache(cachefile, whois_data):
|
||||
pkl_file = open(cachefile, 'wb')
|
||||
pickle.dump(whois_data, pkl_file)
|
||||
pkl_file.close()
|
||||
|
||||
|
||||
def usage():
|
||||
print('%s version %1.1f\n' % (__PROGNAME__, __version__))
|
||||
print('usage: %s [-aeqhCx] [-s server] [-p port] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
|
||||
print('%s version %s\n' % (__progname__, __version__))
|
||||
print('usage: %s [-aeqhCx] [-s server] [-p port] [-c count] [-t type] [-w wait] hostname' % __progname__)
|
||||
print(' -h --help Show this help')
|
||||
print(' -q --quiet Quiet')
|
||||
print(' -x --expert Print expert hints if available')
|
||||
@ -303,7 +311,7 @@ def main():
|
||||
trace_path = []
|
||||
|
||||
if not quiet:
|
||||
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dest_port, hostname, dnsrecord),
|
||||
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__progname__, dnsserver, dest_port, hostname, dnsrecord),
|
||||
flush=True)
|
||||
|
||||
while True:
|
||||
@ -367,11 +375,11 @@ def main():
|
||||
if curr_addr:
|
||||
as_name = ""
|
||||
if as_lookup:
|
||||
asn = whoisrecord(curr_addr)
|
||||
asn = whois_lookup(curr_addr)
|
||||
as_name = ''
|
||||
try:
|
||||
if asn and asn.asn != "NA":
|
||||
as_name = "[%s %s] " % (asn.asn, asn.owner)
|
||||
as_name = "[AS%s %s] " % (asn.asn, asn.owner)
|
||||
except AttributeError:
|
||||
if shutdown:
|
||||
sys.exit(0)
|
||||
@ -404,7 +412,7 @@ def main():
|
||||
|
||||
if __name__ == '__main__':
|
||||
try:
|
||||
whois_cache = load_whois_cache(WHOIS_CACHE)
|
||||
main()
|
||||
finally:
|
||||
pkl_file = open(WHOIS_CACHE, 'wb')
|
||||
pickle.dump(whois, pkl_file)
|
||||
save_whois_cache(WHOIS_CACHE, whois_cache)
|
||||
|
@ -1 +1,2 @@
|
||||
dnspython==1.15.0
|
||||
dnspython>=1.15.0
|
||||
cymruwhois>=1.6
|
||||
|
5
setup.py
5
setup.py
@ -2,8 +2,11 @@ from setuptools import setup, find_packages
|
||||
|
||||
setup(
|
||||
name="dnsdiag",
|
||||
version="1.6.0",
|
||||
version="1.6.3",
|
||||
packages=find_packages(),
|
||||
scripts=["dnseval.py", "dnsping.py", "dnstraceroute.py"],
|
||||
install_requires=['dnspython>=1.15.0', 'cymruwhois>=1.6'],
|
||||
|
||||
classifiers=[
|
||||
"Topic :: System :: Networking",
|
||||
"Environment :: Console",
|
||||
|
Reference in New Issue
Block a user