Add option to pause between each dnsping request

This commit is contained in:
Hamish Coleman 2016-08-04 14:13:33 +10:00
parent ec6e93e2b2
commit df366d5934

View File

@ -58,6 +58,7 @@ usage: %s [-h] [-q] [-v] [-s server] [-p port] [-P port] [-S address] [-c count]
-S --srcip Query source IP address (default: default interface address) -S --srcip Query source IP address (default: default interface address)
-c --count Number of requests to send (default: 10) -c --count Number of requests to send (default: 10)
-w --wait Maximum wait time for a reply (default: 5) -w --wait Maximum wait time for a reply (default: 5)
-i --interval Time between each request (default: 0)
-t --type DNS request record type (default: A) -t --type DNS request record type (default: A)
-e --edns Use EDNS0 -e --edns Use EDNS0
""" % (__PROGNAME__, __VERSION__, __PROGNAME__)) """ % (__PROGNAME__, __VERSION__, __PROGNAME__))
@ -85,6 +86,7 @@ def main():
dnsrecord = 'A' dnsrecord = 'A'
count = 10 count = 10
timeout = 5 timeout = 5
interval = 0
quiet = False quiet = False
verbose = False verbose = False
dnsserver = dns.resolver.get_default_resolver().nameservers[0] dnsserver = dns.resolver.get_default_resolver().nameservers[0]
@ -97,8 +99,8 @@ def main():
hostname = 'wikipedia.org' hostname = 'wikipedia.org'
try: try:
opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:vp:P:S:T46e", opts, args = getopt.getopt(sys.argv[1:], "qhc:s:t:w:i:vp:P:S:T46e",
["help", "count=", "server=", "quiet", "type=", "wait=", "verbose", ["help", "count=", "server=", "quiet", "type=", "wait=", "interval=", "verbose",
"port=", "srcip=", "tcp", "ipv4", "ipv6", "srcport=", "edns"]) "port=", "srcip=", "tcp", "ipv4", "ipv6", "srcport=", "edns"])
except getopt.GetoptError as err: except getopt.GetoptError as err:
# print help information and exit: # print help information and exit:
@ -126,6 +128,8 @@ def main():
verbose = False verbose = False
elif o in ("-w", "--wait"): elif o in ("-w", "--wait"):
timeout = int(a) timeout = int(a)
elif o in ("-i", "--interval"):
interval = int(a)
elif o in ("-t", "--type"): elif o in ("-t", "--type"):
dnsrecord = a dnsrecord = a
elif o in ("-T", "--tcp"): elif o in ("-T", "--tcp"):
@ -209,6 +213,10 @@ def main():
print(answers.rrset) print(answers.rrset)
print("flags:", dns.flags.to_text(answers.response.flags)) print("flags:", dns.flags.to_text(answers.response.flags))
time_to_next = (stime + interval) - etime
if time_to_next > 0:
time.sleep(time_to_next)
r_sent = i + 1 r_sent = i + 1
r_received = len(response_time) r_received = len(response_time)
r_lost = r_sent - r_received r_lost = r_sent - r_received