@echo off &setlocal enabledelayedexpansion
echo 修改hosts文件
::type c:\windows\system32\drivers\etc\hosts^ | findstr /n ".*"
:main
echo  ===============================
echo   请选择需要进行的操作
echo  ===============================
echo.
echo  1. 添加hosts       2. 修改hosts
echo.
echo  3. 清空hosts       4. 显示当前hosts内容
echo.
echo  5. 退出            6. 清屏 
choice /c:123456 /m:请输入数字:
if errorlevel 6 goto clear
if errorlevel 5 goto end
if errorlevel 4 goto type
if errorlevel 3 goto del
if errorlevel 2 goto change
if errorlevel 1 goto add

:add
echo  -------------------------------
set /p domain=请输入域名:
echo  -------------------------------
set /p ip=请输入IP:
echo  -------------------------------
echo %ip% %domain% >>c:\windows\system32\drivers\etc\hosts
if errorlevel 0 (echo 修改成功) else (echo 修改失败)
choice /c:123 /m:1返回,2ping测试,3退出:
if errorlevel 3 goto end
if errorlevel 2 goto ping
if errorlevel 1 goto main
:ping
echo ----------------开始进行ping测试----------------
for /f "delims=[] tokens=2 " %%i in ('ping -n 1 %domain%^|findstr [') do (if %ip% == %%i (echo ----------------ping测试成功----------------) else (echo ----------------ping测试失败,请重新修改----------------))
goto choice
:type
echo 当前hosts内容:
echo  -------------------------------
type c:\windows\system32\drivers\etc\hosts^ | findstr /n ".*"
echo  -------------------------------
echo.
goto choice

:clear
cls
goto main
:del
del /Q c:\windows\system32\drivers\etc\hosts
if errorlevel 0 (echo 清除成功) else (echo 清除失败)
type nul >c:\windows\system32\drivers\etc\hosts

goto choice
:choice
choice /c:12 /m:继续请按1,退出请按2
REM 应先判断数值最高的错误码
if errorlevel 2 goto end
if errorlevel 1 goto main
:end
exit