批处理修改hosts
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了批处理修改hosts相关的知识,希望对你有一定的参考价值。
怎么用批处理修改hosts域名解析文件啊?
C:\WINDOWS\system32\drivers\etc\hosts
例如加个:
0.0.0.0 www.qq.com
(一定要批处理)
@echo off & title Photoshop CS4\5 联网后注册码无效解决方案
echo 请按任意键执行此方案……
pause>nul
set lj="%systemroot%\System32\drivers\etc\hosts"
echo # Copyright (c) 1993-2006 Microsoft Corp.>%lj%
echo #>>%lj%
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>>%lj%
echo #>>%lj%
echo # This file contains the mappings of IP addresses to host names. Each>>%lj%
echo # entry should be kept on an individual line. The IP address should>>%lj%
echo # be placed in the first column followed by the corresponding host name.>"%lj%
echo # The IP address and the host name should be separated by at least one>>%lj%
echo # space.>>%lj%
echo #>>%lj%
echo # Additionally, comments (such as these) may be inserted on individual>>%lj%
echo # lines or following the machine name denoted by a '#' symbol.>>%lj%
echo #>>%lj%
echo # For example:>>%lj%
echo #>>%lj%
echo # 102.54.94.97 rhino.acme.com # source server>>%lj%
echo # 38.25.63.10 x.acme.com # x client host>>%lj%
echo.>>%lj%
echo 127.0.0.1 localhost>>%lj%
echo ::1 localhost>>%lj%
echo 127.0.0.1 activate.adobe.com >>%lj%
echo 127.0.0.1 practivate.adobe.com >>%lj%
echo 127.0.0.1 ereg.adobe.com >>%lj%
echo 127.0.0.1 activate.wip3.adobe.com >>%lj%
echo 127.0.0.1 wip3.adobe.com >>%lj%
echo 127.0.0.1 3dns-3.adobe.com >>%lj%
echo 127.0.0.1 3dns-2.adobe.com >>%lj%
echo 127.0.0.1 adobe-dns.adobe.com >>%lj%
echo 127.0.0.1 adobe-dns-2.adobe.com >>%lj%
echo 127.0.0.1 adobe-dns-3.adobe.com >>%lj%
echo 127.0.0.1 ereg.wip3.adobe.com >>%lj%
echo 127.0.0.1 activate-sea.adobe.com >>%lj%
echo 127.0.0.1 wwis-dubc1-vip60.adobe.com >>%lj%
echo 127.0.0.1 activate-sjc0.adobe.com >>%lj%
echo 方案执行完毕!请按回车键退出……
pause>nul
这样就可以达到修改hosts文件的目的,如果是在Vista或WIN7下,要用管理员身份运行,让批处理得到足够的修改权限。
好了,就这样 参考技术A 通过批处理命令就可以修改hosts文件。下面的例子中,通过批处理命令中的管道命令和逻辑判断,修改hosts文件中原来的映射。
@echo off
cmd /c
cd /d c:
cd %windir%\system32\drivers\etc
for /f "tokens=1,*" %%i in (hosts) do ((echo %%i%%j|find /I "www.dynamipsgui.net")||(echo %%i%%j|find /I "www.dynamipsgui.cn")||echo %%i %%j>>hostsnew.txt)
echo A.B.C.D www.dynamipsgui.net >>hostsnew.txt
echo W.X.Y.Z www.dynamipsgui.cn>>hostsnew.txt
if exist hosts.bak del hosts.bak
rename hosts hosts.bak
rename hostsnew.txt hosts
rem pause
另外还需要注意,只有系统管理员权限才可以修改hosts文件,如果不想逐个电脑修改,可以通过组策略中的开机或者关机脚本来执行这个批处理修改hosts文件。 参考技术B @echo off
echo.>>"C:\WINDOWS\system32\drivers\etc\hosts"
echo 0.0.0.0 www.qq.com>>"C:\WINDOWS\system32\drivers\etc\hosts" 参考技术C @echo off
echo 0.0.0.0 www.qq.com>>%Windir%\system32\drivers\etc\hosts本回答被提问者采纳 参考技术D @echo off
color 0a
title hosts appender 1.0
SETLOCAL ENABLEDELAYEDEXPANSION
set /a RTN_VAL=0
set ORIGIN_HOSTS=C:\\Windows\\system32\\drivers\\etc\\hosts
set FILE_HOSTS=hosts.tmp
set FOLDER_HOSTS=hosts.tmp.1
if not exist "%ORIGIN_HOSTS%" (
echo Not exist hosts.
goto NORMAL_EXIT
)
set /a RTN_VAL=0
cd /d "%ORIGIN_HOSTS%" && set /a RTN_VAL=1
if !RTN_VAL! equ 1 (
echo Not a hosts file.
goto NORMAL_EXIT
)
cls
if "%TEMP%" neq "" (
cd /d "%TEMP%"
set /a RTN_VAL=0
cd . > "%FILE_HOSTS%" || set /a RTN_VAL=1
if !RTN_VAL! equ 1 (
cls
echo Can not access to "%FILE_HOSTS%".
goto NORMAL_EXIT
)
) else (
echo Environment Variables - TEMP is empty.
goto NORMAL_EXIT
)
:INPUT_COMMENTS
set /p COMMENTS=Enter comments:
if "%COMMENTS%" equ "" (
goto INPUT_COMMENTS
)
:INPUT_IP_ADDRESS
set /p IP_ADDRESS=Enter IP address:
if "%IP_ADDRESS%" equ "" (
goto INPUT_IP_ADDRESS
)
:INPUT_HOST_NAMES
set /p HOST_NAMES=Enter host names:
if "%HOST_NAMES%" equ "" (
goto INPUT_HOST_NAMES
)
>> "%FILE_HOSTS%" echo.
>> "%FILE_HOSTS%" echo # %COMMENTS%
>> "%FILE_HOSTS%" echo %IP_ADDRESS% %HOST_NAMES%
cls
echo Preview:
type "%FILE_HOSTS%"
echo.
:INPUT_APPEND_CONFIRM
set /p APPEND_CONFIRM=Enter 'Y' append to origin hosts, or 'n' quit.":
if "%APPEND_CONFIRM%" equ "Y" (
set /a RTN_VAL=0
type "%FILE_HOSTS%" >> "%ORIGIN_HOSTS%" || set /a RTN_VAL=1
if !RTN_VAL! equ 0 (
echo Completed successfully.
goto NORMAL_EXIT
) else (
cls
if exist "%FOLDER_HOSTS%" (
rmdir /s /q "%FOLDER_HOSTS%"
)
mkdir "%FOLDER_HOSTS%"
type "%ORIGIN_HOSTS%" > "%FOLDER_HOSTS%\\hosts"
type "%FILE_HOSTS%" >> "%FOLDER_HOSTS%\\hosts"
set ORIGIN_ATTR=
for /f "tokens=*" %%I in ("%ORIGIN_HOSTS%") do (
set ORIGIN_ATTR=%%~aI
)
if "!ORIGIN_ATTR!" neq "" (
set /a RTN_VAL=0
for /f "tokens=1* delims=-" %%m in ("!ORIGIN_ATTR!") do (
set ORIGIN_ATTR=%%m
set /a RTN_VAL=1
)
if !RTN_VAL! equ 0 (
set ORIGIN_ATTR=
)
) else (
echo Can not obtain file attributes.
goto NORMAL_EXIT
)
attrib -r -a -s -h -i "%FOLDER_HOSTS%\\hosts"
set SURPLUS=!ORIGIN_ATTR!
set STR_ATTR=
:PROCESS_ATTR
for /f "tokens=*" %%m in ("!SURPLUS!") do (
set STR_ATTR=!STR_ATTR!+!SURPLUS:~0,1!
set SURPLUS=!SURPLUS:~1!
)
if "!SURPLUS!" neq "" (
goto PROCESS_ATTR
)
if "!STR_ATTR!" neq "" (
rem No 'I'.
attrib !STR_ATTR!"%FOLDER_HOSTS%\\hosts"
)
set /a RTN_VAL=0
xcopy /y /q /h /r /k "%FOLDER_HOSTS%\\hosts" "%ORIGIN_HOSTS%" || set /a RTN_VAL=1
if !RTN_VAL! equ 0 (
cls
echo Completed successfully.
goto NORMAL_EXIT
) else (
echo Failed.
goto NORMAL_EXIT
)
)
) else if "%APPEND_CONFIRM%" equ "n" (
goto NORMAL_EXIT
) else (
goto INPUT_APPEND_CONFIRM
)
:NORMAL_EXIT
pause
exit
rms365/miscellaneous
以上是关于批处理修改hosts的主要内容,如果未能解决你的问题,请参考以下文章