I have this batch file that checks to see if a Windows device is at work or off-site. If at work, it uses our internal DNS, and if off-site, it uses a public DNS. The only problem is, I'm using a ping to determine if the devices is on-site or off-site, which takes a few seconds to fail. This would work okay if it did not have to go thru multiple NICs (3 on my devices - Bluetooth, Ethernet NIC and wireless NIC).
This batch file is set off every time a network change event happens (connection to wifi for example). Is there a way that I could do this faster? I'm thinking maybe I can first check if the NIC has an IP (or someway to know the nic is active), and if so, then next test if it's on-site, and finally change the DNS appropriately.
I'm thinking the ping test is the safest way to determine if on-site or off-site because we have wireless IP addresses that are 10.x.x.x and I know some hotels and businesses use that range. Here is currently what I have:
REM If INTERNAL_IP_Check IP is available, it will set network cards to use local DNS.
REM If INTERNAL_IP_Check IP is unreachable, it will set to public DNS
REM --- set DNS Servers
for /f "skip=2 tokens=3*" %%i in ('netsh interface show interface') do (
ping -n 3 %INTERNAL_IP_Check% | find "TTL=" >nul
if errorlevel 1 (
netsh interface ip add dns "%%j" %DNS_SERVER1% validate=no
netsh interface ip add dns "%%j" %DNS_SERVER2% validate=no
) else (
netsh interface ip add dns "%%j" %DNS_SERVER_ALT% validate=no
ipconfig /flushdns >nul 2>&1