Skip to content

Instantly share code, notes, and snippets.

@cat5inthecradle
Created January 27, 2015 21:32
Show Gist options
  • Select an option

  • Save cat5inthecradle/f07575dd2e7c39fa25fd to your computer and use it in GitHub Desktop.

Select an option

Save cat5inthecradle/f07575dd2e7c39fa25fd to your computer and use it in GitHub Desktop.
Hosts File Util
@echo off
REM Usage
REM
REM hostswap.bat <ip address> <hostname>
REM This will add the given information to the hosts file,
REM and save the original file to hosts.bak.
REM
REM hostswap.bat -revert
REM This will swap hosts and hosts.bak.
REM
REM hostswap.bat -reset
REM This will restore the default Windows (empty) hosts file.
set hostfile=c:\windows\system32\drivers\etc\hosts
set hostfilebak=c:\windows\system32\drivers\etc\hosts.bak
set hostfilestaging=c:\windows\system32\drivers\etc\hosts.staging
echo Backup of existing hosts file will be saved as hosts.bak
IF "%1" == "-revert" (
REM backs up the current config and restores the .bak
copy %hostfilebak% %hostfilestaging%
copy %hostfile% %hostfilebak%
copy %hostfilestaging% %hostfile%
del %hostfilestaging%
GOTO :EOF
) ELSE IF "%1" == "-reset" (
REM Resets host file to Windows default
copy %hostfile% %hostfilebak%
del %hostfile%
echo # Copyright ^(c^) 1993-2006 Microsoft Corp. >> %hostfile%
echo # >> %hostfile%
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. >> %hostfile%
echo # >> %hostfile%
echo # This file contains the mappings of IP addresses to host names. Each >> %hostfile%
echo # entry should be kept on an individual line. The IP address should >> %hostfile%
echo # be placed in the first column followed by the corresponding host name. >> %hostfile%
echo # The IP address and the host name should be separated by at least one >> %hostfile%
echo # space. >> %hostfile%
echo # >> %hostfile%
echo # Additionally, comments ^(such as these^) may be inserted on individual >> %hostfile%
echo # lines or following the machine name denoted by a '#' symbol. >> %hostfile%
echo # >> %hostfile%
echo # For example: >> %hostfile%
echo # >> %hostfile%
echo # 102.54.94.97 rhino.acme.com # source server >> %hostfile%
echo # 38.25.63.10 x.acme.com # x client host >> %hostfile%
echo # localhost name resolution is handle within DNS itself. >> %hostfile%
echo # 127.0.0.1 localhost >> %hostfile%
echo # ::1 localhost >> %hostfile%
GOTO :EOF
)
) ELSE (
REM backs up the current config and adds the arguments as a new entry
copy %hostfile% %hostfilebak%
echo %* >> %hostfile%
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment