Advertisement
OldSparta

.bat file for hosts

Feb 21st, 2012
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.72 KB | None | 0 0
  1. @echo off
  2. rem ### Toggle HOSTS File Entries #########
  3. rem # Version 20120221-1 by Scott Garrett #
  4. rem # Wintervenom [(at)] archlinux.us #
  5. rem #######################################
  6.  
  7. rem ###############
  8. rem ### Globals ###
  9. rem ###############
  10.  
  11. rem Path to the HOSTS file.
  12. set hosts=%SYSTEMROOT%\system32\drivers\etc\HOSTS
  13.  
  14. rem Which IP should this script toggle the entries of?
  15. rem This will be used as a regular expression, so the
  16. rem octet delimiters must be escaped.
  17. set ip=208.43.40.120
  18.  
  19. rem What domains should point to the IP defined above?
  20. set tld=dreamviews.com
  21. set domains=(%tld% www.%tld% irc.%tld% webchat.%tld%)
  22.  
  23.  
  24.  
  25. rem ############
  26. rem ### Main ###
  27. rem ############
  28.  
  29. echo Toggle HOSTS File Entries
  30. echo Version 20120221-1 by Scott Garrett
  31. echo Wintervenom [(at)] archlinux.us
  32. echo.
  33.  
  34. rem Look for existing entries. If they are found, assume the user is
  35. rem running this script again to remove them.
  36. findstr /b /m /l "%ip%" %hosts%
  37. set add_entries=%ERRORLEVEL%
  38.  
  39. rem Can't modify the HOSTS file without the script being executed as
  40. rem Administrator. Let's check for that.
  41. >nul 2>&1 cacls %SYSTEMROOT%\system32\config\system
  42. if not errorlevel 0 (
  43. echo You will need to execute this script as administrator to use it.
  44. pause
  45. exit 1
  46. )
  47.  
  48. if exist %hosts% (
  49. rem Back up the original HOSTS file, just in case.
  50. echo Backing up current HOSTS file to "HOSTS.old"...
  51. copy /y %hosts% %hosts%.old || (
  52. echo Could not back up your HOSTS file - error code %ERRORLEVEL%.
  53. echo Press [Enter] if you like to try to edit the entries, anyway, or
  54. echo Press [Control]-[C] to bail out of here.
  55. pause
  56. )
  57.  
  58. echo.
  59. rem Remove existing entries pointing to the address in %IP%.
  60. rem We can't redirect to the same file that is being read, so we'll
  61. rem write to a different file...
  62. echo Getting rid of existing entries pointing to %ip%...
  63. findstr /b /v /l "%ip%" %hosts% > %hosts%.new
  64. rem ...then replace the old one.
  65. move /y %hosts%.new %hosts%
  66. ) ELSE (
  67. rem If, for some reason, the user doesn't have a HOSTS file, then we
  68. rem need to create one.
  69. echo For some reason, you do not have a HOSTS file.
  70. echo One will be created for you.
  71. echo 127.0.0.1 localhost > %hosts%
  72. echo ::1 localhost >> %hosts%
  73. )
  74.  
  75. rem Append new entries for the domain listed in the %domains% array if
  76. rem there were no entries previously found.
  77. set action=removed
  78. if %add_entries% EQU 0 goto the_end
  79.  
  80. echo.
  81. set action=added
  82. for %%h in %domains% do (
  83. echo %ip% %%h >> %hosts%
  84. echo Added entry: %ip% %%h
  85. )
  86.  
  87. :the_end
  88. echo.
  89. echo HOSTS file entries have been %action% for %ip%.
  90. echo Run this script again to toggle.
  91. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement