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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/bin/env python3
#
# Copyright (c) 2016-2024, Babak Farrokhi
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# * Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# * Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import datetime
import getopt
import ipaddress
import json
import os
import socket
import sys
import dns.rcode
import dns.rdatatype
import dns.resolver
import util.dns
__author__ = 'Babak Farrokhi (babak@farrokhi.net)'
__license__ = 'BSD'
__progname__ = os.path.basename(sys.argv[0])
from util.dns import PROTO_UDP, PROTO_TCP, PROTO_TLS, PROTO_HTTPS, setup_signal_handler, flags_to_text
from util.shared import __version__, Colors
def usage():
print("""%s version %s
Usage: %s [-ehmvCTXH] [-f server-list] [-j output.json] [-c count] [-t type] [-p port] [-w wait] hostname
-h, --help Display this help message
-f, --file Specify a DNS server list file to use (default: system resolvers)
-c, --count Number of requests to send (default: 10)
-m, --cache-miss Force a cache miss measurement by prepending a random hostname
-w, --wait Set the maximum wait time for a reply in seconds (default: 2)
-t, --type Set the DNS request record type (default: A)
-T, --tcp Use TCP as the transport protocol instead of UDP
-X, --tls Use TLS as the transport protocol
-j, --json Save the results to a specified file in JSON format
-H, --doh Use HTTPS as the transport protocol (DoH)
-p, --port Specify the DNS server port number (default: 53 for TCP/UDP, 853 for TLS)
-S, --srcip Set the query source IP address
-e, --edns Enable EDNS0 in requests
-D, --dnssec Enable the 'DNSSEC desired' (DO flag) in requests
-C, --color Enable colorful output
-v, --verbose Print the full DNS response details
""" % (__progname__, __version__, __progname__))
sys.exit()
def maxlen(names):
sn = sorted(names, key=len)
return len(sn[-1])
def main():
setup_signal_handler()
if len(sys.argv) == 1:
usage()
# defaults
rdatatype = 'A'
proto = PROTO_UDP
src_ip = None
dst_port = 53 # default for UDP and TCP
count = 10
waittime = 2
inputfilename = None
fromfile = False
json_output = False
use_edns = False
want_dnssec = False
force_miss = False
verbose = False
color_mode = False
qname = 'wikipedia.org'
try:
opts, args = getopt.getopt(sys.argv[1:], "hf:c:t:w:S:TevCmXHDj:p:",
["help", "file=", "count=", "type=", "wait=", "json=", "tcp", "edns", "verbose",
"color", "cache-miss", "srcip=", "tls", "doh", "dnssec", "port="])
except getopt.GetoptError as err:
print(err)
usage()
if args and len(args) == 1:
qname = args[0]
else:
usage()
for o, a in opts:
if o in ("-h", "--help"):
usage()
elif o in ("-c", "--count"):
count = int(a)
elif o in ("-f", "--file"):
inputfilename = a
fromfile = True
elif o in ("-w", "--wait"):
waittime = int(a)
elif o in ("-m", "--cache-miss"):
force_miss = True
elif o in ("-t", "--type"):
rdatatype = a
elif o in ("-T", "--tcp"):
proto = PROTO_TCP
elif o in ("-S", "--srcip"):
src_ip = a
elif o in ("-j", "--json"):
json_output = True
json_filename = a
elif o in ("-e", "--edns"):
use_edns = True
elif o in ("-D", "--dnssec"):
want_dnssec = True
use_edns = True # implied
elif o in ("-C", "--color"):
color_mode = True
elif o in ("-v", "--verbose"):
verbose = True
elif o in ("-X", "--tls"):
proto = PROTO_TLS
dst_port = 853 # default for DoT, unless overridden using -p
elif o in ("-H", "--doh"):
proto = PROTO_HTTPS
dst_port = 443 # default for DoH, unless overridden using -p
elif o in ("-p", "--port"):
dst_port = int(a)
else:
print("Invalid option: %s" % o)
usage()
# validate RR type
if not util.dns.valid_rdatatype(rdatatype):
print('Error: Invalid record type "%s" ' % rdatatype)
sys.exit(1)
color = Colors(color_mode)
try:
if fromfile:
if inputfilename == '-':
# read from stdin
with sys.stdin as flist:
f = flist.read().splitlines()
else:
try:
with open(inputfilename, 'rt') as flist:
f = flist.read().splitlines()
except Exception as e:
print(e)
sys.exit(1)
else:
f = dns.resolver.get_default_resolver().nameservers
if len(f) == 0:
print("Error: No nameserver specified")
f = [name.strip() for name in f] # remove annoying blanks
f = [x for x in f if not x.startswith('#') and len(x)] # remove comments and empty entries
width = maxlen(f)
blanks = (width - 5) * ' '
if not json_output:
print('server ', blanks,
' avg(ms) min(ms) max(ms) stddev(ms) lost(%) ttl flags response')
print((104 + width) * '-')
for server in f:
# check if we have a valid dns server address
if server.lstrip() == '': # deal with empty lines
continue
server = server.replace(' ', '')
try:
ipaddress.ip_address(server)
except ValueError: # so it is not a valid IPv4 or IPv6 address, so try to resolve host name
try:
resolver = socket.getaddrinfo(server, port=None)[1][4][0]
except OSError:
print('Error: cannot resolve hostname:', server)
resolver = None
except Exception:
pass
else:
resolver = server
if not resolver:
continue
try:
retval = util.dns.ping(qname, resolver, dst_port, rdatatype, waittime, count, proto, src_ip,
use_edns=use_edns, force_miss=force_miss, want_dnssec=want_dnssec)
except SystemExit:
break
except Exception as e:
print('%s: %s' % (server, e))
continue
resolver = server.ljust(width + 1)
text_flags = flags_to_text(retval.flags)
s_ttl = str(retval.ttl)
if s_ttl == "None":
s_ttl = "N/A"
if retval.r_lost_percent > 0:
l_color = color.O
else:
l_color = color.N
if json_output:
dns_data = {
'hostname': qname,
'timestamp': str(datetime.datetime.now()),
'r_min': retval.r_min,
'r_avg': retval.r_avg,
'resolver': resolver.rstrip(),
'r_max': retval.r_max,
'r_lost_percent': retval.r_lost_percent,
's_ttl': s_ttl,
'text_flags': text_flags,
'flags': retval.flags,
'rcode': retval.rcode,
'rcode_text': retval.rcode_text,
}
outer_data = {
'hostname': qname,
'data': dns_data
}
if json_filename == '-': # stdout
print(json.dumps(outer_data, indent=2))
else:
with open(json_filename, 'a+') as outfile:
json.dump(outer_data, outfile, indent=2)
else:
result = "%s %-8.3f %-8.3f %-8.3f %-8.3f %s%%%-3d%s %-8s %21s %-20s" % (
resolver, retval.r_avg, retval.r_min, retval.r_max, retval.r_stddev, l_color, retval.r_lost_percent,
color.N, s_ttl, text_flags, retval.rcode_text)
print(result.rstrip(), flush=True)
if verbose and retval.answer and not json_output:
ans_index = 1
for answer in retval.answer:
print("Answer %d [ %s%s%s ]" % (ans_index, color.G, answer, color.N))
ans_index += 1
print("")
except Exception as e:
print('%s: %s' % (server, e))
sys.exit(1)
if __name__ == '__main__':
main()
|