Last active
January 3, 2016 10:49
-
-
Save vladimirnani/8451608 to your computer and use it in GitHub Desktop.
Swithces redirections in hosts file based on environment variable.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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