windows batchを使用して、このバッチスクリプトを使用して、ホストファイルに新しいエントリを自動的に追加します。
残念ながら、スクリプトは、ホストファイルに1行追加するだけで、管理者としてスクリプトを実行するときにも何が問題になりますか?
@echo off
set hostspath=%windir%\System32\drivers\etc\hosts
echo 62.116.159.4 ns1.intranet.de >> %hostspath%
echo 217.160.113.37 ns2.intranet.de >> %hostpath%
echo 89.146.248.4 ns3.intranet.de >> %hostpath%
echo 74.208.254.4 ns4.intranet.de >> %hostpath%
exit
このようにするので、スクリプトが複数回実行されても、エントリが重複することはありません。
@echo off
SET NEWLINE=^& echo.
FIND /C /I "ns1.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^62.116.159.4 ns1.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns2.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^217.160.113.37 ns2.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns3.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^89.146.248.4 ns3.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
FIND /C /I "ns4.intranet.de" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^74.208.254.4 ns4.intranet.de>>%WINDIR%\System32\drivers\etc\hosts
単純なタイプミス。 hostspath
vs hostpath
;)
@echo off
set `hostspath`=%windir%\System32\drivers\etc\hosts
echo 62.116.159.4 ns1.intranet.de >> `%hostspath%`
echo 217.160.113.37 ns2.intranet.de >> `%hostpath%`
echo 89.146.248.4 ns3.intranet.de >> `%hostpath%`
echo 74.208.254.4 ns4.intranet.de >> `%hostpath%`
exit
これが上記の@rashyの私の修正です。スクリプトは次のことを行います。
これはスクリプトです:
@echo off
TITLE Modifying your HOSTS file
COLOR F0
ECHO.
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop
:REJECTED
ECHO Your HOSTS file was left unchanged>>%systemroot%\Temp\hostFileUpdate.log
ECHO Finished.
GOTO END
:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of Host domains
set LIST=(diqc.oca wiki.oca)
::Set the ip of the domains you set in the list above
set diqc.oca=192.168.111.6
set wiki.oca=192.168.111.4
:: deletes the parentheses from LIST
set _list=%LIST:~1,-1%
::ECHO %WINDIR%\System32\drivers\etc\hosts > tmp.txt
for %%G in (%_list%) do (
set _name=%%G
set _value=!%%G!
SET NEWLINE=^& echo.
ECHO Carrying out requested modifications to your HOSTS file
::strip out this specific line and store in tmp file
type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_name! > tmp.txt
::re-add the line to it
ECHO %NEWLINE%^!_value! !_name!>>tmp.txt
::overwrite Host file
copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts
del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
ECHO You should now open Chrome and go to "chrome://net-internals/#dns" (without quotes)
ECHO then click the "clear Host cache" button
GOTO END
:END
ECHO.
ping -n 11 192.0.2.2 > nul
EXIT
次の内容を含む新しいaddHostEntry.batファイルを作成します。
@echo off
TITLE Modifying your HOSTS file
COLOR F0
ECHO.
:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop
:REJECTED
ECHO Your HOSTS file was left unchanged>>%systemroot%\Temp\hostFileUpdate.log
ECHO Finished.
GOTO END
:ACCEPTED
SET NEWLINE=^& echo.
ECHO Carrying out requested modifications to your HOSTS file
FIND /C /I "mydomain.com" %WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%>>%WINDIR%\system32\drivers\etc\hosts
IF %ERRORLEVEL% NEQ 0 ECHO 127.0.0.1 mydomain.com>>%WINDIR%\system32\drivers\etc\hosts
ECHO Finished
GOTO END
:END
ECHO.
ping -n 11 127.0.0.1 > nul
EXIT
お役に立てれば!
他の誰かがホストエントリセットを通常のホストファイルのような形式のtxtファイルに保存したい場合に備えて、この回答を追加しています。これは、TAB区切り文字を探します。これは、@ Rashyと@ that0n3guyからの回答に基づいています。 FORコマンドの前後で違いに気付くことができます。
@echo off
TITLE Modifying your HOSTS file
ECHO.
:: BatchGotAdmin
:-------------------------------------
REM --> Check for permissions
>nul 2>&1 "%SYSTEMROOT%\system32\cacls.exe" "%SYSTEMROOT%\system32\config\system"
REM --> If error flag set, we do not have admin.
if '%errorlevel%' NEQ '0' (
echo Requesting administrative privileges...
goto UACPrompt
) else ( goto gotAdmin )
:UACPrompt
echo Set UAC = CreateObject^("Shell.Application"^) > "%temp%\getadmin.vbs"
set params = %*:"="
echo UAC.ShellExecute "cmd.exe", "/c %~s0 %params%", "", "runas", 1 >> "%temp%\getadmin.vbs"
"%temp%\getadmin.vbs"
del "%temp%\getadmin.vbs"
exit /B
:gotAdmin
pushd "%CD%"
CD /D "%~dp0"
:--------------------------------------
:LOOP
SET Choice=
SET /P Choice="Do you want to modify HOSTS file ? (Y/N)"
IF NOT '%Choice%'=='' SET Choice=%Choice:~0,1%
ECHO.
IF /I '%Choice%'=='Y' GOTO ACCEPTED
IF /I '%Choice%'=='N' GOTO REJECTED
ECHO Please type Y (for Yes) or N (for No) to proceed!
ECHO.
GOTO Loop
:REJECTED
ECHO Your HOSTS file was left unchanged.
ECHO Finished.
GOTO END
:ACCEPTED
setlocal enabledelayedexpansion
::Create your list of Host domains
for /F "tokens=1,2 delims= " %%A in (%WINDIR%\System32\drivers\etc\storedhosts.txt) do (
SET _Host=%%B
SET _ip=%%A
SET NEWLINE=^& echo.
ECHO Adding !_ip! !_Host!
REM REM ::strip out this specific line and store in tmp file
type %WINDIR%\System32\drivers\etc\hosts | findstr /v !_Host! > tmp.txt
REM REM ::re-add the line to it
ECHO %NEWLINE%^!_ip! !_Host! >> tmp.txt
REM ::overwrite Host file
copy /b/v/y tmp.txt %WINDIR%\System32\drivers\etc\hosts
del tmp.txt
)
ipconfig /flushdns
ECHO.
ECHO.
ECHO Finished, you may close this window now.
GOTO END
:END
ECHO.
PAUSE
EXIT
例 "storedhosts.txt"(タブ区切り)
127.0.0.1 mysite.com
168.1.64.2 yoursite.com
192.1.0.1 internalsite.com