用批处理修改host文件的代码
程序员文章站
2022-07-06 12:50:57
@echo 127.0.0.1 baidu.com >>c:\windows\system32\drivers\etc\hosts 这样就是换一行写入。 如果还...
@echo 127.0.0.1 baidu.com >>c:\windows\system32\drivers\etc\hosts
这样就是换一行写入。
如果还想换一行,就写:
@echo. >>c:\windows\system32\drivers\etc\hosts
@echo 127.0.0.1 baidu.com >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.201 ibmrac1 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.202 ibmrac2 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.203 vip-ibmrac1 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.204 vip-ibmrac2>>c:\windows\system32\drivers\etc\hosts
@echo 172.16.1.53 ssosvr>>c:\windows\system32\drivers\etc\hosts
@echo off
set sthosts=172.16.1.53 ssosvr
for /f "eol=# tokens=1 delims=" %%i in (%systemroot%\system32\drivers\etc\hosts) do if "%sthosts%"=="%%i" exit
echo %sthosts%>> %systemroot%\system32\drivers\etc\hosts
由于测试需要经常修改本机host,可以用脚本修改:
分别做两个批处理,一个内网,一个外网,用的时候切换运行即可
如内网.bat
cd /d %windir%\system32\drivers\etc
del hosts.bak //删除原来备份文件
ren hosts hosts.bak //备份现在host
for /f "eol=# tokens=1,2" %%i in (hosts.bak) do call :checkvalue %%i %%j
goto end
:checkvalue
echo %2 | find /i "szshequ.com" //寻找制定记录删除
if %errorlevel%==1 (echo %1 %2 >>hosts)
:end
type c:\windows\system32\drivers\etc\hosts
type c:\windows\system32\drivers\etc\hosts | find "szshequ.com" && goto yes
::echo 192.168.2.188 szshequ.com >> ""
echo 192.168.2.188 szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 www.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 bbs.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 cost.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 pp.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 yh.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 club.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 shop.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 images.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 files.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 ajax.szshequ.com >> c:\windows\system32\drivers\etc\hosts
这样就是换一行写入。
如果还想换一行,就写:
复制代码 代码如下:
@echo. >>c:\windows\system32\drivers\etc\hosts
@echo 127.0.0.1 baidu.com >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.201 ibmrac1 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.202 ibmrac2 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.203 vip-ibmrac1 >>c:\windows\system32\drivers\etc\hosts
@echo 192.168.4.204 vip-ibmrac2>>c:\windows\system32\drivers\etc\hosts
@echo 172.16.1.53 ssosvr>>c:\windows\system32\drivers\etc\hosts
复制代码 代码如下:
@echo off
set sthosts=172.16.1.53 ssosvr
for /f "eol=# tokens=1 delims=" %%i in (%systemroot%\system32\drivers\etc\hosts) do if "%sthosts%"=="%%i" exit
echo %sthosts%>> %systemroot%\system32\drivers\etc\hosts
由于测试需要经常修改本机host,可以用脚本修改:
分别做两个批处理,一个内网,一个外网,用的时候切换运行即可
如内网.bat
复制代码 代码如下:
cd /d %windir%\system32\drivers\etc
del hosts.bak //删除原来备份文件
ren hosts hosts.bak //备份现在host
for /f "eol=# tokens=1,2" %%i in (hosts.bak) do call :checkvalue %%i %%j
goto end
:checkvalue
echo %2 | find /i "szshequ.com" //寻找制定记录删除
if %errorlevel%==1 (echo %1 %2 >>hosts)
:end
type c:\windows\system32\drivers\etc\hosts
type c:\windows\system32\drivers\etc\hosts | find "szshequ.com" && goto yes
::echo 192.168.2.188 szshequ.com >> ""
echo 192.168.2.188 szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 www.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 bbs.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 cost.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 pp.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 yh.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 club.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 shop.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 images.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 files.szshequ.com >> c:\windows\system32\drivers\etc\hosts
echo 192.168.2.188 ajax.szshequ.com >> c:\windows\system32\drivers\etc\hosts