回复 1# MF9593 - @echo off
- @setlocal enabledelayedexpansion
-
- rem 目前支持 add , backup , quit
- rem 格式:
- rem add 域名 [IP地址]
- rem backup
- rem quit
-
- rem Ps : []中的内容表示非必须
- rem 我有空再慢慢打磨 , 添加 del 和 restore
-
- set host="%windir%\system32\drivers\etc\hosts"
- set back="%windir%\system32\drivers\etc\hosts_%date:~0,4%_%date:~5,2%_%date:~8,2%_%time:~0,2%_%time:~3,2%.bak"
-
- rem set host="hosts"
- rem set back="hosts_%date:~0,4%_%date:~5,2%_%date:~8,2%_%time:~0,2%_%time:~3,2%.bak"
-
- attrib -R %host%
-
- :main
- rem 主循环
- set "command="
- set /P command=
- if "%command%"=="" (
- goto:main
- )
- for /F "tokens=1-26" %%a in ("%command%") do (
- if /I "%%a"=="add" call:add %%b %%c
- if /I "%%a"=="backup" call:backup
- if /I "%%a"=="quit" call:quit
- echo done
- )
- goto:main
-
- rem ----------
-
- :add
- rem 添加IP和域名
- rem 若IP缺省则通过ping获取
- set address=%~1
- if "%~2"=="" (
- call:get_IP %address%
- ) else (
- set ip=%~2
- )
- echo %ip% %address% >>%host%
- goto:eof
-
- :get_IP
- for /F "skip=6" %%i in ('ping %~1') do (
- set IP=%%i
- goto:eof
- )
- goto:eof
-
- rem ----------
-
- :backup
- copy %host% %back%
- goto:eof
-
- rem ----------
-
- :quit
- attrib +R %host%
- exit
复制代码
|