How to test if DNS is up (home setup) -- nslookup...?

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
esuhl
Posts: 9
Joined: 04 Feb 2017 22:03

How to test if DNS is up (home setup) -- nslookup...?

#1 Post by esuhl » 25 Feb 2017 22:05

I'm writing a fun little script to test internet connectivity for home users of normal ISPs.

So far I've been using the ping command to ping a domain name, and if it succeeds, I assume that DNS is working. But I've realised that this might just use the local machine's DNS cache without actually testing the DNS server.

I thought the solution might be to use nslookup instead of ping, but... would that just end up checking the DNS cache on the router without actually testing the DNS server...?

At the moment, my ping command looks like this:

Code: Select all

ping -n 1 -w %PingTimeout% %DomainName% | find "TTL=" >nul
if errorlevel 1 (
   REM DNS is down
   ...
)


Ideally I'd like the code to work with any language settings... if that's possible...

Can anyone suggest how I could improve upon my code?

Squashman
Expert
Posts: 4479
Joined: 23 Dec 2011 13:59

Re: How to test if DNS is up (home setup) -- nslookup...?

#2 Post by Squashman » 25 Feb 2017 23:21

Why not use?

Code: Select all

ipconfig /flushdns


But why not just ping an IP address instead?

esuhl
Posts: 9
Joined: 04 Feb 2017 22:03

Re: How to test if DNS is up (home setup) -- nslookup...?

#3 Post by esuhl » 26 Feb 2017 20:34

Squashman wrote:Why not use?

Code: Select all

ipconfig /flushdns


Well, the script is going to be repeating the test every 20 seconds or so. It's designed to be run in the background while the user is still using the PC, so I thought that it might not be "best practice" to just wipe the DNS cache so frequently.

Squashman wrote:But why not just ping an IP address instead?


The script does that too. It pings the router/gateway to see if the LAN is up, pings a WAN IP address to see if the WAN is up, then I want it to test whether DNS is up.

Thanks for your help :-)

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: How to test if DNS is up (home setup) -- nslookup...?

#4 Post by penpen » 27 Feb 2017 06:34

esuhl wrote:I thought the solution might be to use nslookup instead of ping, but... would that just end up checking the DNS cache on the router without actually testing the DNS server...?
Depending on what you do, nslookup is not using the actual cache:

Code: Select all

:: just use
:: nslookup host dnsServer
nslookup www.google.de 192.168.2.1
:: you could verify this using debug
nslookup -debug www.google.de 192.168.2.1


penpen

igor_andreev
Posts: 16
Joined: 25 Feb 2017 12:55
Location: Russia

Re: How to test if DNS is up (home setup) -- nslookup...?

#5 Post by igor_andreev » 15 Mar 2017 15:37

I tried check online\offline via nslookup instead ping. It works OK.

Code: Select all

@ECHO OFF

CALL :dns_status
IF /I "{%network%}" EQU "{false}" (ECHO Offline & EXIT /B 1)
ECHO Online
EXIT /B 0

:dns_status
rem pseudo boolean :)
SET "network=false"
rem OpenDNS 208.67.XXX.XXX \ Google Public DNS 8.8.X.X \ Norton DNS 199.85.XXX.XX \ YandeX DNS 77.88.X.X
rem findstr search output nslookup.exe for "Addresses:"(!) not one "Address:"
SET /A "R1=%random%%% 12 + 1"
SET /A "R2=%random%%% 10 + 1"
FOR /F "tokens=%R1% delims=\" %%A IN ('ECHO\77.88.8.8\199.85.126.10\8.8.8.8\208.67.222.222\208.67.220.222\199.85.127.10\208.67.220.220\77.88.8.7\199.85.126.20\208.67.222.220\199.85.127.20\8.8.4.4') DO SET "public_dns=%%A"
FOR /F "tokens=%R2% delims=\" %%A IN ('ECHO\ebay.com\hp.com\microsoft.net\mail.ru\yahoo.com\google.com\baidu.cn\aol.com\xnxx.com\whois.arin.net') DO SET "ask_about=%%A"
NSLOOKUP %ask_about% %public_dns% 2>nul| more +2 | findstr /B "Addresses:" >nul 2>&1
IF NOT ERRORLEVEL 1 SET "network=true"
GOTO :eof

Post Reply