1. ホーム
  2. スクリプト・コラム
  3. DOS/BAT

script_tool_for_windows.bat Windows 環境のホストのためのワンクリック配備スクリプト

2022-01-25 23:36:50

コピー&ペーストによる手動でのホストの配置・更新に加え、ワンクリックで簡単にホストを適用できるよう、一連のツールを提供しています。

既存のツールは、以下のプラットフォームで動作します。
Microsoft Windows (.batスクリプト、Qt GUIツール、コンソール(サービスタイプ)ツール)

また、クロスプラットフォームのHozzを使用して、ホストの管理および自動アップデートを行うことをお勧めします。

現在、合計で

Hozz クロスプラットフォーム対応のホスト自動管理・更新ツール。pppofficeのプロジェクトである。

プロジェクトホームページ
プロジェクトGithubトップページ https://github.com/racaljk/hosts/tree/master/hosts_tools
HostTool: Windows 環境の自動ホスト更新アプリケーション、C++ でビルド、Qt ベースの GUI。寄稿者は @racaljk です。
HostToolforMac。Swift ベースの Mac 環境 (OS X 10.10 以上) 用の自動ホスト配置更新アプリケーションです。寄稿者は @ZzzM です。
HostsToolforWindows。Windows コンソール環境におけるホスト用自動更新アプリケーション (システムサービスとして)、C++ で構築されています。寄稿者は @Too-Naive です。
script_tool_for_windows.bat。Windows 環境のホストのためのワンクリック配備スクリプトです。そして、@lideming からの重要な更新です。

@echo off
if defined converted goto :converted

set ConverterPath=%temp%\HostsGeter_CodeConverter.vbs
set ConverterOutput=%temp%\HostsGeter_GBK.bat

echo inputpath="%~0" >%ConverterPath%
echo outputpath="%ConverterOutput%" >>%ConverterPath%
echo set stm2=createobject("ADODB.Stream") >>%ConverterPath%
echo stm2.Charset ="utf-8" >>%ConverterPath%
echo stm2.Open >>%ConverterPath%
echo stm2.LoadFromFile inputpath >>%ConverterPath%
echo readfile = stm2.ReadText >>%ConverterPath%
echo stm2.Close >>%ConverterPath%
echo Set Stm1 =CreateObject("ADODB.Stream") >>%ConverterPath%
echo Stm1.Type = 2 >>%ConverterPath%
echo Stm1.Open >>%ConverterPath%
echo Stm1.Charset = "GBK" >>%ConverterPath%
echo Stm1.Position = Stm1.Size >>%ConverterPath%
echo Stm1.WriteText "set converted=y" ^& vbcrlf >>%ConverterPath%
echo Stm1.WriteText readfile >>%ConverterPath%
echo Stm1.SaveToFile outputpath,2 >>%ConverterPath%
echo Stm1.Close >>%ConverterPath%
%ConverterPath% && %ConverterOutput%
goto :eof

:converted

chcp 936
:: Change cmd window code page to 936(GBK)

cls
%1 %2
ver|find " 5.">nul &&goto :st
echo UAC extraction in progress...
mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st",""," runas",1)(window.close)&goto :eof
:st

cls

@REM HostsGet Version0.4
cd /d %~dp0

set LogFilePath=%temp%\HostsGeter_log.txt
set DLScriptPath=%temp%\downloadhosts.vbs
set DLPath=%windir%\system32\drivers\etc\hosts_downloaded
set BackupDir=%windir%\system32\drivers\etc
set HostsPath=%windir%\system32\drivers\etc\hosts

set LogToFile=^>^>%LogFilePath% 2^>^&1
set EchoAndLog=call :echoandlog
echo. %LogToFile%
echo ==========[%date% %time%]========== %LogToFile%
echo Log file.
echo %LogFilePath%
echo.

echo iLocal=LCase("%DLPath%") > %DLScriptPath% ||(
 call :error downloadhosts.vbs file creation/writing failed.
)
echo iRemote=LCase("https://raw.githubusercontent.com/racaljk/hosts/master/hosts") >> %DLScriptPath%
echo Set xPost=createObject("Microsoft.XMLHTTP") 'Set Post = CreateObject("Msxml2.XMLHTTP") >> %DLScriptPath%
echo xPost.Open "GET",iRemote,0 >> %DLScriptPath%
echo xPost.Send() >> %DLScriptPath%
echo set sGet=createObject("ADODB.Stream") >> %DLScriptPath%
echo sGet.Mode=3 >> %DLScriptPath%
echo sGet.Type=1 >> %DLScriptPath%
echo sGet.Open() >> %DLScriptPath%
echo sGet.Write xPost.ResponseBody >> %DLScriptPath%
echo sGet.SaveToFile iLocal,2 >> %DLScriptPath%

%EchoAndLog% Start downloading the hosts file from GitHub...
if exist %DLPath% del %DLPath% /s /q %LogToFile%
%DLScriptPath% || call :error hosts file download failed.
del %DLScriptPath% /s /q %LogToFile%
if not exist %DLPath% call :error hosts File download failed.
%EchoAndLog% Download completed.
echo.

if exist %HostsPath% (
  call :backuphosts
) else (
  %EchoAndLog% (original hosts file does not exist, skip backup)
)
%EchoAndLog% is replacing the hosts file...
move %DLPath% %HostsPath% %LogToFile% || call :error hosts file replacement failed.
%EchoAndLog% hosts file has been replaced.
echo.

%EchoAndLog% is flushing the system DNS cache...
ipconfig /flushdns %LogToFile% || call :error DNS cache flush failed.
%EchoAndLog% The DNS cache has been flushed.
echo.
%EchoAndLog% [All operations complete!
echo.

echo Press any key to visit google.com.hk to test, if you cancel, please just close this window
pause >nul
start https://www.google.com.hk
echo You can access google.com.hk if you can, then the replacement is successful.
echo.
goto :end

:backuphosts
%EchoAndLog% is backing up the original hosts file...
set "bakfilename=hosts_%date%_%time:~0,8%.bak"
set bakfilename=%bakfilename:/=-%
set bakfilename=%bakfilename:\=-%
set bakfilename=%bakfilename::=-%
set bakfilename=%bakfilename: =_%
copy %HostsPath% %BackupDir%\%bakfilename% %LogToFile% || call :error hosts File backup failed.
%EchoAndLog% The original hosts file was backed up to %BackupDir%\%bakfilename%.
echo.
echo. goto :eof

:error
echo ======================
%EchoAndLog% error: %*
start %LogFilePath%
echo Log file is open
goto :end

:echoandlog
echo %*
echo %* %LogToFile%
goto :eof

:end
echo Press any key to close
pause >nul
exit