有些时候教用户本地修改host,遇到权限不足的问题,让他改权限跟下载编辑器都十分麻烦,
于是这个脚本诞生了,方便新增host指向
复制并保存为 changehost.bat
用管理员权限打开即可
@echo off
chcp 65001 >nul
setlocal enabledelayedexpansion
REM Check for administrator privileges
>nul 2>&1 "%SystemRoot%\system32\cacls.exe" "%SystemRoot%\system32\config\system"
if '%errorlevel%' NEQ '0' (
echo 请以管理员身份运行此脚本.
pause
goto :eof
)
REM Get user input for IP address and domain
set /p addentry="请输入IP地址和域名(例如:127.0.0.1 xiadmin.com): "
REM Display the entry and confirm addition
echo 您输入的条目为: %addentry%
set /p confirm="确认添加到hosts文件吗?(Y/N): "
if /I "%confirm%" NEQ "Y" (
echo 操作已取消
goto :eof
)
REM Set the path to the hosts file
set hostspath=%SystemRoot%\System32\drivers\etc\hosts
REM Check if the entry already exists in the hosts file
findstr /c:"%addentry%" %hostspath% >nul
if %errorlevel% EQU 0 (
echo 条目已存在于hosts文件中
goto :eof
)
REM Prepare temp file and append the entry to the hosts file with a new line
set tempfile=%TEMP%\hosts_temp.txt
copy /Y %hostspath% %tempfile% >nul
(
echo.
echo %addentry%
) >> %tempfile%
REM Replace original hosts file with temp file
copy /Y %tempfile% %hostspath% >nul
if %errorlevel% EQU 0 (
echo 已成功添加到hosts文件
) else (
echo 添加到hosts文件失败
del %tempfile%
goto :eof
)
REM Clean up temp file
del %tempfile%
pause
endlocal
goto :eof