您现在的位置是:网站首页> 编程资料编程资料
用批处理修改host文件的代码_DOS/BAT_
2023-05-25
332人已围观
简介 用批处理修改host文件的代码_DOS/BAT_
@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
相关内容
- CMDOW 一个CMD命令行下 隐藏、禁用窗口控制程序_DOS/BAT_
- Win7+xp命令行 一键修改IP、DNS_DOS/BAT_
- CMD命令行下修改网络IP设置的方法_DOS/BAT_
- Windows下用命令行修改IP地址的方法详解(附批处理文件)_DOS/BAT_
- dos(cmd)中删除、添加、修改注册表命令_DOS/BAT_
- dos命令 cd命令使用说明[图文说明]_DOS/BAT_
- dos命令行中cd命令用法_DOS/BAT_
- dos进程查看与停止命令分享(tasklist,taskkill)_DOS/BAT_
- 巧用DOS命令合并多个文本文件的内容_DOS/BAT_
- 7zip在dos命令行用法总结_DOS/BAT_
