Facebook
From Bitty Echidna, 7 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 276
  1. @ECHO OFF
  2. :: This will add an ipv4 entry to the hosts file, redirecting ipv6 requests to ipv4
  3. :: This must be run as administrator
  4. SET domain=apollo.rip
  5. SET NEWLINE=^& echo.
  6. SET hostsfile="%WINDIR%system32driversetchosts"
  7.  
  8. :: check if you set the domain
  9. IF %domain%  EQU ididntsetthis (
  10.     ECHO ERROR: You didn't set the domain in this file. Change "ididntsetthis" to some domain.
  11.     ECHO i.e. SET domain=google.com
  12.     goto:eof
  13. )
  14.  
  15. :: check if administrater
  16. NET SESSION >nul 2>&1
  17. IF NOT %ERRORLEVEL%  EQU 0 (
  18.     ECHO ERROR: You didn't run this script with administrative privleges.
  19.     ECHO It will not be able to write to the hosts file.
  20.     goto:eof
  21. )
  22.  
  23. :: get ipv4 address
  24. FOR /f "tokens=1,3 delims=: " %%A IN ('ping -n 1 %domain% -4'') DO IF %%A==Reply SET ipaddress=%%B
  25.  
  26. ECHO Uupdating the following to hosts file
  27.  
  28. :: remove existing entry
  29. findstr /v %domain% %hostsfile% > %hostsfile%.tmp
  30. move /Y %hostsfile%.tmp %hostsfile%
  31.  
  32. :: append hosts file
  33. FIND /C /I "%domain%" %hostsfile%
  34. IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^%ipaddress% %domain% >> %hostsfile%
  35.  
  36. ECHO Hosts updated with %ipaddress%     %domain%