Get DNS resolution time with BATCH (cmd)

10,761

Update: C.J. Jackson rightfully notes that when measuring DNS resolution times, you have to account for caching. I thought this would be a given when answering and focussed on the aspect of measuring command runtime. But sure, you'd need to do ipconfig /flushdns to clear the local DNS cache between runs, to really measure your DNS servers performance.


nslookup performs a name resolution against a DNS server - so this is the right command to use. But as you already noted, it does not display the time it takes for the name resolution. It's possible to do that with Windows' own powershell:

powershell "Measure-Command { nslookup www.google.es }"

Now, this outputs on my system...

Non-authoritative answer:


Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 0
Milliseconds      : 31
Ticks             : 317898
TotalDays         : 3,679375E-07
TotalHours        : 8,8305E-06
TotalMinutes      : 0,00052983
TotalSeconds      : 0,0317898
TotalMilliseconds : 31,7898

If you want to use this value programatically you'd filter with FINDSTR:

powershell "Measure-Command { nslookup www.google.es }" | FINDSTR "^Milliseconds"

Unfortunately, this still outputs Non-authoritative answer: which is printed by nslookup to STDERR. In contrast to batch, you can't redirect to NUL in powershell - instead you need to redirect to $null:

powershell "Measure-Command { nslookup www.google.es 2> $null }" | FINDSTR "^Milliseconds"

Output:

Milliseconds      : 31

Alternatively, as pointed out by Stephan, you could also move the redirection outside to the powershell invocation and thus out of the time measurement (which amounts to ~2ms difference on my end). This works since powershell outputs to STDOUT instead of STDERR:

powershell "Measure-Command { nslookup www.google.es }" 2>NUL | FINDSTR "^Milliseconds"
Share:
10,761
MarcSerrano93
Author by

MarcSerrano93

Updated on June 18, 2022

Comments

  • MarcSerrano93
    MarcSerrano93 almost 2 years

    I'm trying to obtain the resolution time of my DNS server, but I'm not sure that I'm doing it well.

    First of all I tried to make a ping to my DNS server IP, and catch the time value, but i'm not sure that it's the correct value because I think that this value is the time to make a request and get a response to this IP.

    Then I tried to make 2 diferent pings, one like this: "ping www.google.es" and the second one like: "ping googleIP" and compare the time but the time is exactly the same.

    Finally I tried to search one command that gives me this information but I can't find it. I tried the nslookup but it isn'tthe answer that I need. Does there exist any command or any way to get this information?

    Thank you. :)

  • Stephan
    Stephan over 8 years
    move the redirection outside the powershell: ...google.es }" 2>nul |findstr..., so it isn't measured at all.
  • zb226
    zb226 over 8 years
    @Stephan: I just noticed the >NUL redirection causes an error in powershell - and amended the answer accordingly.
  • Stephan
    Stephan over 8 years
    Indeed - that explains the very different times. Adapted my previous comment.
  • zb226
    zb226 over 8 years
    @Stephan: Excellent, I've included that in the answer.
  • C.J. Jackson
    C.J. Jackson over 5 years
    Doesn't DNS get cached after the first lookup? So would you have to do ipconfig /flushdns between each searches?
  • zb226
    zb226 over 5 years
    @C.J.Jackson Sure, DNS caching will be at play. But the question here was how to time nslookup. Still, re-reading the whole thing, adding a note felt like the right thing to do :)