Batch files: How to check if DNS is up?

9.4K Posts


in Techie Stuff
I'm writing a little batch file (for fun), which repeatedly checks the PC's connection to the LAN gateway, to a WAN IP, and to DNS. It'll be running in domestic situations, rather than commercial ones.
I can ping the gateway and WAN IP to see whether they're connected, and was going to just ping a domain name to check DNS... and then I realised that might use the DNS cache and succeed even when the DNS servers are down.
I could use ipconfig /flushdns first, but I want to repeatedly run the test every 20 seconds or so in the background, and wiping the cache so frequently doesn't seem like "best practise".
Apparently nslookup can be used to check DNS resolution, but when I ran a quick test, it seems to get results even when the WAN was disconnected (presumably via a DNS cache on the router?).
Anyway, I just wondered if anyone had any hints or suggestions...?
Maybe if there's a way to get the IP addresses of the DNS servers used by the router, I could ping them directly...?
I can ping the gateway and WAN IP to see whether they're connected, and was going to just ping a domain name to check DNS... and then I realised that might use the DNS cache and succeed even when the DNS servers are down.
I could use ipconfig /flushdns first, but I want to repeatedly run the test every 20 seconds or so in the background, and wiping the cache so frequently doesn't seem like "best practise".
Apparently nslookup can be used to check DNS resolution, but when I ran a quick test, it seems to get results even when the WAN was disconnected (presumably via a DNS cache on the router?).
Anyway, I just wondered if anyone had any hints or suggestions...?

Maybe if there's a way to get the IP addresses of the DNS servers used by the router, I could ping them directly...?
0
This discussion has been closed.
Latest MSE News and Guides
Replies
As for checking connection status, this is something I did very recently to force my home server to recognise when a connection problem has occurred, so that it could shutdown and restart various services that can and do go nuts if the internet drops out.
Realised that ping.exe can return a non-error status when the router responds to outgoing ping requests, even if there is no internet connection.
So I had to use:
ping -n -w 500 www.google.com|findstr /t /c:"timed out" /c:"unreachable" /c:"could not be found"
To give me the correct %errorlevel%
<><><><><><><><><<><><><><><><><><><><><><> Don't forget to like and subscribe \/ \/ \/
https://www.grc.com/dns/benchmark.htm
One way of disabling DNS cache http://www.tothepc.com/archives/turn-off-dns-cache-on-windows/ A second would be to limit the ttl to 1 second.
This is the ping you want http://nirsoft.net/utils/multiple_ping_tool.html
This is the ping you wish you wrote, but it is windows version dependent
http://nirsoft.net/utils/dns_records_viewer.html
****************************************************************************************
Honestly I cant figure out exactly what you are trying to do, and why, and why you want to make your internet even slower by disabling cache?
like Stryder590, I too had network issues. I kept loosing connection. It was easy to blame the isp, too easy. For starters my router connection was rock solid with no unplanned disconnects. I use homePlugs that are wired and have been in the past rock solid. these had a damaged looking nic cable, but replacing it did not fix the issue.
Second form of attack: since my network was dropping was set a continuous ping, as the first internet rule is to blame the isp. So setup a continuous ping on my mobile to BBC. If my pc drops and isp drops, my phone should verify this drop of pings. My PC regularly lost connection, but my mobile kept pinging like a trooper (did remember to disable Mobile Data), so problem was my network, cabling, or PC or windows, and not my isp
I started with the stuff that needed less physical effort first - PC. (1) newest nic drivers from manufacturers site and a small reliability difference was noted. (2) installed the free http://www.iobit.com/en/driver-booster.php and checked for outdated windows drivers. found a few newer whq drivers, and these are not part of windows updates. After installation everything was rock solid again. (3) next step would have been to put my windows PC on wifi and linux PC onto the homeplugs (4) depending on previous outcome; would either be a 20m eithernet wired direct, or a bootable mint usb stick plugged into my widows laptop.
If the first DNS server is down, or running slowly, the website URL will be resolved to an IP address by the second DNS server. (Actually, for Google and other large DNS providers their "8.8.8.8" server will be a whole bunch of servers, rotated, which we don't know about.)
Unless you're running your own DNS server internally, I don't know what you'll get from your exercise!
[For interest, you might like to look at Net Uptime Monitor - free for less than an hour each time you run it, but $9.95 for a licence.]
nslookup will presumably default to your normal dns server, which would be your router one. But you can tell it to query any dns server directly. (Well, you can in the linux one - can't see why the windows one would be any different.) So you could bypass your router one and look straight at the ISP one (or the google one, or whichever one you're trying to monitor.)
Could just configure your router with enough redundant servers that if all are missing, the whole internet is pretty much out of action.
(But polling an upstream dns server every 20 seconds seems like serious overkill.)
Just to check whether it's working. I once had an ISP who had intermittent issues with their DNS servers. WAN access was available, but domain resolution wasn't. I thought it might be useful to check for this condition...?
Ah -- good point!
Interesting... I'm looking for "TTL=" in the output to see if a domain is up. Like this:
I do the same thing to ping the LAN gateway/router.
I don't want to disable the DNS cache. I want to test that DNS is up.
Ah... It's quite possible I'm trying to do something stupid. I just thought that, when diagnosing connectivity problems, it might be a good way to determine where the fault lies: with the LAN (by pinging the gateway), with the WAN (by pinging a public IP address), or with DNS (by... well... I don't know... pinging a domain name? using nslookup to get a realtime DNS resolution...?)
I just want to test whether public domain names (e.g. google.co.uk) can be resolved without relying on the DNS cache -- i.e. whether DNS is "working".
I intend the script to run in domestic environments, so yes -- normally the router would be running a DHCP server to allocate IP addresses and act as a gateway for DNS requests, which would be stored in the router's firmware settings. Usually these would be the DNS servers supplied by the ISP, but they could be Google's or OpenDNS's or whatever. Also, it's possible that the DNS settings might have been set locally. Either way, I just want to test that DNS is up.
Is this such a crazy idea?
Essentially, I'm writing a little tool, mostly for fun, but partly so that I can give it to other people if they find it useful. So cheers for the suggestion, but I want to try to write my own script if I can.
My internet connection was going up and down at random, so I wrote a simple script to ping the LAN and/or WAN and put the results in a log file. It seemed to make sense to test every 20 seconds to get a vaguely accurate time. Maybe a little bit overkill :-)
Anyway, the script did what I needed it to and an engineer came out pretty quickly to fix the problem. Now I'm just trying to add as many bells and whistles to the script that I can think of!
Oh, wait... :eek: