Skip to content

Instantly share code, notes, and snippets.

@vladimirnani
Last active January 3, 2016 10:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimirnani/8451608 to your computer and use it in GitHub Desktop.
Save vladimirnani/8451608 to your computer and use it in GitHub Desktop.
Swithces redirections in hosts file based on environment variable.
SET InputFile=%SystemRoot%\System32\drivers\etc\hosts
SET OutputFile=%SystemRoot%\System32\drivers\etc\hoststemp
IF "%HOSTS_OVERRIDE%" == "" GOTO FIRST_RUN
IF "%HOSTS_OVERRIDE%"=="1" (
SETX HOSTS_OVERRIDE 0
GOTO TEST
) else (
SETX HOSTS_OVERRIDE 1
GOTO LIVE
)
:FIRST_RUN
SETX HOSTS_OVERRIDE 0
ECHO # LIVEDMZ>>%InputFile%
GOTO TEST
:TEST
SET "_strFind=# LIVEDMZ"
SET "_strInsert=10.249.103.7 apps.be.katoennatie.com"
GOTO REPLACE_HOST
:LIVE
SET "_strFind=10.249.103.7 apps.be.katoennatie.com"
SET "_strInsert=# LIVEDMZ"
GOTO REPLACE_HOST
:REPLACE_HOST
@ECHO OFF
SETLOCAL DISABLEDELAYEDEXPANSION
>"%OutputFile%" (
FOR /f "usebackq delims=" %%A in ("%InputFile%") do (
ECHO.%%A | FINDSTR /C:"%_strFind%" 1>nul
IF errorlevel 1 (ECHO %%A) ELSE (ECHO %_strInsert%)
)
)
XCOPY /Q /Y /R %OutputFile% %InputFile%
:END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment