add ability to define arbitrary src port and IP address

This commit is contained in:
Babak Farrokhi 2016-04-16 14:11:38 +04:30
parent 20d007d672
commit 98e0a8199f
Signed by: farrokhi
GPG Key ID: 6B267AD85D632E9A

View File

@ -43,17 +43,19 @@ should_stop = False
def usage(): def usage():
print('%s version %1.1f\n' % (__PROGNAME__, __VERSION__)) print("""%s version %1.1f
print('syntax: %s [-h] [-q] [-v] [-s server] [-p port] [-c count] [-t type] [-w wait] hostname' % __PROGNAME__) syntax: %s [-h] [-q] [-v] [-s server] [-p port] [-P port] [-S address] [-c count] [-t type] [-w wait] hostname
print(' -h --help Show this help') -h --help Show this help
print(' -q --quiet Quiet') -q --quiet Quiet
print(' -v --verbose Print actual dns response') -v --verbose Print actual dns response
print(' -s --server DNS server to use (default: 8.8.8.8)') -s --server DNS server to use (default: 8.8.8.8)
print(' -p --port DNS server port number (default: 53)') -p --port DNS server port number (default: 53)
print(' -c --count Number of requests to send (default: 10)') -P --srcport Query source port number (default: 0)
print(' -w --wait Maximum wait time for a reply (default: 5)') -S --srcip Query source IP address (default: default interface address)
print(' -t --type DNS request record type (default: A)') -c --count Number of requests to send (default: 10)
print(' ') -w --wait Maximum wait time for a reply (default: 5)
-t --type DNS request record type (default: A)
""" % (__PROGNAME__, __VERSION__, __PROGNAME__))
exit() exit()
@ -68,7 +70,7 @@ def main():
try: try:
signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z signal.signal(signal.SIGTSTP, signal.SIG_IGN) # ignore CTRL+Z
signal.signal(signal.SIGINT, signal_handler) # custom CTRL+C handler signal.signal(signal.SIGINT, signal_handler) # custom CTRL+C handler
except AttributeError: # OS Does not support some signals, probably windows except AttributeError: # OS Does not support some signals, probably windows
pass pass
if len(sys.argv) == 1: if len(sys.argv) == 1:
@ -82,12 +84,14 @@ def main():
verbose = False verbose = False
dnsserver = '8.8.8.8' dnsserver = '8.8.8.8'
dest_port = 53 dest_port = 53
src_port = 0
src_ip = None
hostname = 'wikipedia.org' hostname = 'wikipedia.org'
try: try:
opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:vp:", opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:vp:P:S:",
["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "verbose", ["help", "output=", "count=", "server=", "quiet", "type=", "wait=", "verbose",
"port"]) "port", "dstport=", "srcip="])
except getopt.GetoptError as err: except getopt.GetoptError as err:
# print help information and exit: # print help information and exit:
print(err) # will print something like "option -a not recognized" print(err) # will print something like "option -a not recognized"
@ -116,6 +120,12 @@ def main():
timeout = int(a) timeout = int(a)
elif o in ("-t", "--type"): elif o in ("-t", "--type"):
dnsrecord = a dnsrecord = a
elif o in ("-P", "--srcport"):
src_port = int(a)
if src_port < 1024:
print("WARNING: Source ports below 1024 are only available to superuser")
elif o in ("-S", "--srcip"):
src_ip = a
else: else:
usage() usage()
@ -146,7 +156,7 @@ def main():
break break
try: try:
stime = time.time() stime = time.time()
answers = resolver.query(hostname, dnsrecord) answers = resolver.query(hostname, dnsrecord, source_port=src_port, source=src_ip)
etime = time.time() etime = time.time()
except dns.resolver.NoNameservers as e: except dns.resolver.NoNameservers as e:
if not quiet: if not quiet: