@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