support for arbitrary dns port for dnstraceroute

and some trivial cleanup and beatufications
This commit is contained in:
Babak Farrokhi 2016-04-12 13:33:54 +04:30
parent d43b03909e
commit d90cc4a8d5
Signed by: farrokhi
GPG Key ID: 6B267AD85D632E9A
3 changed files with 18 additions and 14 deletions

View File

@ -25,8 +25,6 @@
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
import dns.rdatatype
import dns.resolver
import getopt
import os
import signal
@ -34,6 +32,9 @@ import sys
import time
from statistics import stdev
import dns.rdatatype
import dns.resolver
__VERSION__ = 1.0
__PROGNAME__ = os.path.basename(sys.argv[0])
should_stop = False

View File

@ -37,14 +37,14 @@ from statistics import stdev
import dns.rdatatype
import dns.resolver
__VERSION__ = 1.0
__VERSION__ = 1.1
__PROGNAME__ = os.path.basename(sys.argv[0])
should_stop = False
def usage():
print('%s version %1.1f\n' % (__PROGNAME__, __VERSION__))
print('syntax: %s [-h] [-q] [-v] [-s server] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
print('syntax: %s [-h] [-q] [-v] [-s server] [-p port] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
print(' -h --help Show this help')
print(' -q --quiet Quiet')
print(' -v --verbose Print actual dns response')
@ -83,7 +83,8 @@ def main():
try:
opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:vp:",
["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "verbose", "port"])
["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "verbose",
"port"])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
@ -136,7 +137,6 @@ def main():
i = 0
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dest_port, hostname, dnsrecord))
for i in range(count):
if should_stop:

View File

@ -44,7 +44,7 @@ except ImportError:
has_whois = False
# Constants
__VERSION__ = 1.0
__VERSION__ = 1.1
__PROGNAME__ = os.path.basename(sys.argv[0])
WHOIS_CACHE = 'whois.cache'
@ -85,11 +85,12 @@ if has_whois:
def usage():
print('%s version %1.1f\n' % (__PROGNAME__, __VERSION__))
print('syntax: %s [-h] [-q] [-s server] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
print('syntax: %s [-h] [-q] [-a] [-s server] [-p port] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__)
print(' -h --help Show this help')
print(' -q --quiet Quiet')
print(' -a --asn Turn on AS# lookups for each hop encountered')
print(' -s --server DNS server to use (default: first system resolver)')
print(' -p --port DNS server port number (default: 53)')
print(' -c --count Maximum number of hops (default: 30)')
print(' -w --wait Maximum wait time for a reply (default: 5)')
print(' -t --type DNS request record type (default: A)')
@ -116,13 +117,13 @@ def main():
timeout = 1
quiet = False
dnsserver = dns.resolver.get_default_resolver().nameservers[0]
dnsport = 53
dest_port = 53
hops = 0
as_lookup = False
try:
opts, args = getopt.getopt(sys.argv[1:], "aqhc:s:t:w:",
["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "asn"])
opts, args = getopt.getopt(sys.argv[1:], "aqhc:s:t:w:p:",
["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "asn", "port"])
except getopt.GetoptError as err:
# print help information and exit:
print(err) # will print something like "option -a not recognized"
@ -146,6 +147,8 @@ def main():
timeout = int(a)
elif o in ("-t", "--type"):
dnsrecord = a
elif o in ("-p", "--port"):
dest_port = int(a)
elif o in ("-a", "--asn"):
if has_whois:
as_lookup = True
@ -165,7 +168,7 @@ def main():
ttl = 1
reached = False
print("%s %s: hostname=%s rdatatype=%s" % (__PROGNAME__, dnsserver, hostname, dnsrecord))
print("%s DNS: %s:%d, hostname: %s, rdatatype: %s" % (__PROGNAME__, dnsserver, dest_port, hostname, dnsrecord))
while True:
if should_stop:
@ -182,7 +185,7 @@ def main():
print("Error: Unable to create ICMP socket with unprivileged user. Please run as root.")
exit(1)
icmp_socket.bind(("", dnsport))
icmp_socket.bind(("", dest_port))
icmp_socket.settimeout(timeout)
try: # send DNS request
@ -252,7 +255,7 @@ def main():
except AttributeError:
if should_stop:
exit(0)
pass
pass
print("%d\t%s (%s) %s%d ms" % (ttl, curr_name, curr_addr, as_name, elapsed))
else: