第一种:
echo off
chcp 65001
set sp0=------------------------
set sp1=host助手
set hostsfile="%SYSTEMROOT%\System32\Drivers\etc\hosts"
title %sp1%
color 5f
:mainmenu
echo %sp0%
echo %sp1%
echo %sp0%
echo 1.编辑hosts 2.恢复hosts
echo 3.打开hosts文件 4.退出
echo.
set input=
set /p input=请选择:
if "%input%" == "" goto :mainmenu
if "%input%" =="1" goto :writehost
if "%input%" =="2" goto :recovery
if "%input%" =="3" goto :showhosts
if "%input%" =="4" goto :endtime
echo.
:writehost
cls
echo %sp0%
echo %sp0%
echo 120.0.0.1 www.baidu.com >> "%hostsfile%"
echo 编写完毕
echo %sp0%
echo %sp0%
echo 任意键回到主界面
echo.
pause >nul
goto :mainmenu
:recovery
cls
echo %sp0%
echo %sp0%
echo # Copyright (c) 1993-2009 Microsoft Corp.>hosts
echo #>>hosts
echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>>hosts
echo #>>hosts
echo # This file contains the mappings of IP addresses to host names. Each>>hosts
echo # entry should be kept on an individual line. The IP address should>>hosts
echo # be placed in the first column followed by the corresponding host name.>>hosts
echo # The IP address and the host name should be separated by at least one>>hosts
echo # space.>>hosts
echo #>>hosts
echo # Additionally, comments (such as these) may be inserted on individual>>hosts
echo # lines or following the machine name denoted by a '#' symbol.>>hosts
echo #>>hosts
echo # For example:>>hosts
echo #>>hosts
echo # 102.54.94.97 rhino.acme.com # source server>>hosts
echo # 38.25.63.10 x.acme.com # x client host>>hosts
echo.>>hosts
echo # localhost name resolution is handled within DNS itself.>>hosts
echo # 127.0.0.1 localhost>>hosts
echo # ::1 localhost>>hosts
echo.>>hosts
copy hosts C:\Windows\System32\drivers\etc\
del hosts
echo hosts已恢复完毕
echo %sp0%
echo %sp0%
echo 任意键回到主界面
echo.
pause >nul
goto :mainmenu
:showhosts
cls
start "" notepad.exe %hostsfile%
echo 文件已打开
echo %sp0%
echo %sp0%
echo 任意键回到主界面
echo.
pause >nul
goto :mainmenu
:endtime
exit
第二种:通过文件内容复制来恢复修改的hosts
echo off
chcp 65001
set sp0=------------------------
set sp1=host助手
set hostsfile="%SYSTEMROOT%\System32\Drivers\etc\hosts"
title %sp1%
color 5f
:mainmenu
cls
echo %sp0%
echo %sp1%
echo %sp0%
echo 1.编辑hosts
echo 2.查看hosts
echo 3.退出
echo.
set input=
set /p input=请选择:
if "%input%" == "" goto :endtime
if "%input%" =="1" goto :writehost
if "%input%" =="2" goto :showhosts
if "%input%" =="3" goto :endtime
echo.
:writehost
copy %hostsfile% G:\back.txt
echo 备份完毕
echo %sp0%
echo 120.0.0.1 www.tutu.com >> "%hostsfile%"
echo %sp0%
echo 编写完毕,任意键恢复hosts
pause > nul
goto:back
:showhosts
start "" notepad.exe %hostsfile%
:endtime
exit /B
:back
copy G:\back.txt %hostsfile%
echo 编写完毕,任意键回到主菜单
pause > nul
goto:mainmenu