有部分软件在使用过程中会修改host,我就写出这个还原host的批处理。- @echo off&cls
- echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
- echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
- attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
- cd.>%windir%\system32\drivers\etc\hosts
- echo # Copyright (c) 1993-1999 Microsoft Corp.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # This is a sample HOSTS file used by Microsoft TCP/IP for Windows.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # This file contains the mappings of IP addresses to host names. Each>>%windir%\system32\drivers\etc\hosts
- echo # entry should be kept on an individual line. The IP address should>>%windir%\system32\drivers\etc\hosts
- echo # be placed in the first column followed by the corresponding host name.>>%windir%\system32\drivers\etc\hosts
- echo # The IP address and the host name should be separated by at least one>>%windir%\system32\drivers\etc\hosts
- echo # space.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # Additionally, comments (such as these) may be inserted on individual>>%windir%\system32\drivers\etc\hosts
- echo # lines or following the machine name denoted by a '#' symbol.>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # For example:>>%windir%\system32\drivers\etc\hosts
- echo #>>%windir%\system32\drivers\etc\hosts
- echo # 102.54.94.97 rhino.acme.com # source server>>%windir%\system32\drivers\etc\hosts
- echo # 38.25.63.10 x.acme.com # x client host>>%windir%\system32\drivers\etc\hosts
- echo.>>%windir%\system32\drivers\etc\hosts
- echo 127.0.0.1 localhost>>%windir%\system32\drivers\etc\hosts
复制代码 运行后与系统原来host没有差别。
后来觉得这个批处理语句太多重复句子。
例如:“echo”与“%windir%\system32\drivers\etc\hosts”。
想精简批处理。
后来用for尝试。- @echo off
- cls
- echo y|cacls %windir%\system32\drivers\etc\hosts /c /p administrators:f>nul
- echo y|cacls %windir%\system32\drivers\etc\hosts /e /c /p "power users:r" users:r system:f>nul
- attrib -h -r -s +a %windir%\system32\drivers\etc\hosts>nul
- cd.>%windir%\system32\drivers\etc\hosts
- for %%1 in (
- "# Copyright (c) 1993-1999 Microsoft Corp."
- "#"
- "# This is a sample HOSTS file used by Microsoft TCP/IP for Windows."
- "#"
- "# This file contains the mappings of IP addresses to host names. Each"
- "# entry should be kept on an individual line. The IP address should"
- "# be placed in the first column followed by the corresponding host name."
- "# The IP address and the host name should be separated by at least one"
- "# space."
- "#"
- "# Additionally, comments (such as these) may be inserted on individual"
- "# lines or following the machine name denoted by a "#" symbol."
- "#"
- "# For example:"
- "#"
- "# 102.54.94.97 rhino.acme.com # source server"
- "# 38.25.63.10 x.acme.com # x client host"
- ""
- "127.0.0.1 localhost"
- ) do echo %%~1 >>%windir%\system32\drivers\etc\hosts
- pause
复制代码 但结果是每行最后都有一个空格,而且不能输出空行。(“” host结果显示 ECHO 处于关闭状态。) |