We'd like to remind Forumites to please avoid political debate on the Forum... Read More »
Batch files: How to check if DNS is up?

esuhl
Posts: 9,409 Forumite


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
Comments
-
Not sure why you want to check DNS, for a start ping.exe doesn't use the same port as DNS requests, so while the IP maybe the same, it won't tell you whether the DNS server is down or not.
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%“I may not agree with you, but I will defend to the death your right to make an a** of yourself.”
<><><><><><><><><<><><><><><><><><><><><><> Don't forget to like and subscribe \/ \/ \/0 -
just because you are able to ping the DNS ip, does not mean is going to resolve the dns request
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 dependentping -t bbc.co.uk|cmd /q /v /c "(pause&pause)>nul & for /l %a in () do (set /p "data=" && echo(!time! !data!)&ping -n 2 localhost>nul" >> d:\pingtest.txt
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.0 -
I'm of the same mind as Strider590, and don't quite see what you're trying to achieve. You have (presumably?) set up manually, or DHCP has set up for you, two external DNS server IP addresses in your adapter's IPv4 parameters. Example: Google DNS 8.8.8.8 and 8.8.4.4
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.]0 -
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?).
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.)0 -
Strider590 wrote: »Not sure why you want to check DNS...
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...?Strider590 wrote: »... for a start ping.exe doesn't use the same port as DNS requests, so while the IP maybe the same, it won't tell you whether the DNS server is down or not.
Ah -- good point!Strider590 wrote: »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 https://www.google.com|findstr /t /c:"timed out" /c:"unreachable" /c:"could not be found"
To give me the correct %errorlevel%
Interesting... I'm looking for "TTL=" in the output to see if a domain is up. Like this:ping -n 1 -w %PingTimeout% %Domain1% | find "TTL=" >nul if errorlevel 1 ( REM Domain not available ... )
I do the same thing to ping the LAN gateway/router.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.
...
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?
I don't want to disable the DNS cache. I want to test that DNS is up.I'm of the same mind as Strider590, and don't quite see what you're trying to achieve.
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...?)You have (presumably?) set up manually, or DHCP has set up for you, two external DNS server IP addresses in your adapter's IPv4 parameters. Example: Google DNS 8.8.8.8 and 8.8.4.4
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!
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?[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.]
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.psychic_teabag wrote: »(But polling an upstream dns server every 20 seconds seems like serious overkill.)
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!0 -
This discussion has been closed.
Confirm your email address to Create Threads and Reply

Categories
- All Categories
- 349.8K Banking & Borrowing
- 252.6K Reduce Debt & Boost Income
- 453K Spending & Discounts
- 242.8K Work, Benefits & Business
- 619.6K Mortgages, Homes & Bills
- 176.4K Life & Family
- 255.7K Travel & Transport
- 1.5M Hobbies & Leisure
- 16.1K Discuss & Feedback
- 15.1K Coronavirus Support Boards