#1 11 Dec 2019 17:08

wowSS64hasAforum
Member
Registered: 11 Dec 2019
Posts: 3

Instant DNS override/unblock through Hosts file

Because sometimes you need instant access!

This is my first batch script so don't judge. Thanks to ss64 I managed to do it in 4-5hrs lol. Although I have this written way better in PowerShell, I find PS quite annoying with admin rights and all its quirks, especially in a corporate environment. So, I'm sure a lot of you guys have TLDs blocked in your custom DNS system, which is wonderful for privacy reasons, but sometimes you just need to a webpage quickly, perhaps read some documentation? The best part about this is it still keeps the privacy quite intact, while you may have *google.com blocked you can instantly unlock a specific subdomain, like chrome doc's etc while not giving way to google-analytics. Anyways, too much talk, here it is. I'd really appreciate any improvements.

@echo off
setlocal enabledelayedexpansion

dir "%SystemRoot%\System32\config\DRIVERS" 2>nul >nul || echo "Sorry, only admins can write to the hosts file."

set domain=%1
if defined domain (goto dnsquery) else (set /p domain="Enter a Top Level Domain: ")

:dnsquery
for /F "tokens=2 delims=:" %%A in ('nslookup -type^=A -querytype^=A !domain! 1.1.1.1 ^| findstr /R "Address.*[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]*\.[0-9][0-9]"') DO (
	set IP=%%A
	for /F "tokens=1 delims= " %%f in ("!IP!") do (
		set IP=%%~f
		goto hostswriter
	)
)

:hostswriter
set "HOSTS_FILE=%SystemRoot%\System32\drivers\etc\hosts"
echo . >> %HOSTS_FILE%
echo !IP! !domain! >> %HOSTS_FILE%
echo !IP! !domain! "Successfully Written To: " %HOSTS_FILE%
echo "Lets GO!"
endlocal

:EOF

1. Is there an LRU for batch? one could instantly comment out last value?
2. Any easy way to test if string starts with "https://etc.."? so then the script can regex 2nd \/ to 3rd \/ slash and extract the domain?
3. How can this be incorporated directly into aliases? Can you do DOSKEY var=goto subroutine $1 ? Doesn't make any sense to pass the argument that way.

Cheers. Thanks SS64, I just discovered the forum, I always stumbled upon your website but I thought it was some official old-school man page. Cheers!

Offline

#2 11 Dec 2019 17:10

wowSS64hasAforum
Member
Registered: 11 Dec 2019
Posts: 3

Re: Instant DNS override/unblock through Hosts file

Also, why does this forum require "Referer" header? even IETF is discontinuing it.

Offline

#3 13 Dec 2019 16:55

Simon Sheppard
Super Administrator
Registered: 27 Aug 2005
Posts: 1,049
Website

Re: Instant DNS override/unblock through Hosts file

wowSS64hasAforum wrote:

Also, why does this forum require "Referer" header? even IETF is discontinuing it.

The forum is overdue a major upgrade and I think this will happen in the first half of 2020. That 'feature' will definitely be going away.

Offline

Board footer

Powered by FluxBB