侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

script_tool_for_windows.bat Windows 环境下的 hosts 一键部署脚本

Windows  /  管理员 发布于 5年前   255

我们提供了一系列工具让您除了手动以 复制/粘贴 的方式部署和更新 hosts 之外,有一种方便、一键式的方式来应用我们的 hosts 。

我们现有的工具可以在下列平台上工作:
Microsoft Windows (.bat script; Qt GUI tool; console (service type) tool)

另外,我们还推荐使用跨平台的 Hozz 来管理、自动更新 hosts 。

目前,我们共有:

Hozz: 跨平台的 hosts 自动 管理/更新 工具。是 @ppoffice 的项目。

项目主页
项目Github主页 https://github.com/racaljk/hosts/tree/master/hosts_tools
HostTool: Windows 环境下的 hosts 自动更新应用,使用 C++ 构建, GUI 基于 Qt 。由 @racaljk 贡献。
HostToolforMac: Mac 环境下的 hosts 自动部署更新应用(适用于 OS X 10.10 或以上),基于 Swift 开发。由 @ZzzM 贡献。
HostsToolforWindows: Windows console环境下的 hosts 自动更新应用(作为系统服务),使用 C++ 构建。由 @Too-Naive 贡献。
script_tool_for_windows.bat: Windows 环境下的 hosts 一键部署脚本,由 @redapple0204 与 @lizongzeshunshun 贡献。以及 @lideming 提供的重要更新。

@echo offif defined converted goto :convertedset ConverterPath=%temp%\HostsGeter_CodeConverter.vbsset ConverterOutput=%temp%\HostsGeter_GBK.batecho 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:convertedchcp 936:: 更改cmd窗口代码页至 936(GBK)cls%1 %2ver|find " 5.">nul &&goto :stecho 正在进行 UAC 提权...mshta vbscript:createobject("shell.application").shellexecute("%~s0","goto :st","","runas",1)(window.close)&goto :eof:stcls@REM HostsGet Version0.4cd /d %~dp0set LogFilePath=%temp%\HostsGeter_log.txtset DLScriptPath=%temp%\downloadhosts.vbsset DLPath=%windir%\system32\drivers\etc\hosts_downloadedset BackupDir=%windir%\system32\drivers\etcset HostsPath=%windir%\system32\drivers\etc\hostsset LogToFile=^>^>%LogFilePath% 2^>^&1set EchoAndLog=call :echoandlogecho. %LogToFile%echo ==========[%date% %time%]========== %LogToFile%echo 日志文件:echo %LogFilePath%echo.echo iLocal=LCase("%DLPath%") > %DLScriptPath% ||( call :error downloadhosts.vbs 文件创建/写入失败.)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% 开始从 GitHub 中下载 hosts 文件...if exist %DLPath% del %DLPath% /s /q %LogToFile%%DLScriptPath% || call :error hosts 文件下载失败.del %DLScriptPath% /s /q %LogToFile%if not exist %DLPath% call :error hosts 文件下载失败.%EchoAndLog% 下载完成.echo.if exist %HostsPath% (  call :backuphosts) else (  %EchoAndLog% (原 hosts 文件不存在,跳过备份))%EchoAndLog% 正在替换 hosts 文件...move %DLPath% %HostsPath% %LogToFile% || call :error hosts 文件替换失败.%EchoAndLog% hosts 文件已替换.echo.%EchoAndLog% 正在刷新系统 DNS 缓存...ipconfig /flushdns %LogToFile% || call :error DNS 缓存刷新失败.%EchoAndLog% DNS 缓存已刷新.echo.%EchoAndLog% 【操作全部完成!】echo.echo 按任意键访问 google.com.hk 进行测试,如取消,请直接关闭本窗口pause >nulstart https://www.google.com.hkecho 已经帮你访问 google.com.hk 如可以访问则替换成功.echo.goto :end:backuphosts%EchoAndLog% 正在备份原 hosts 文件...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 文件备份失败.%EchoAndLog% 原 hosts 文件已备份到 %BackupDir%\%bakfilename%.echo.goto :eof:errorecho ======================%EchoAndLog% 错误:%*start %LogFilePath%echo 已打开日志文件goto :end:echoandlogecho %*echo %* %LogToFile%goto :eof:endecho 按任意键关闭pause >nulexit


  • 上一条:
    用dos命令实现导入、导出windows计划任务详解
    下一条:
    Windows下通过bat获取网络连线实际名称,加强IP类设置脚本的兼容性
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Windows 10的告别:2025年10月14日,一段时代的终结(0个评论)
    • windows 11激活_Win11 KMS激活流程步骤(1个评论)
    • 安装Windows 11系统的注意了,看看你的cpu是否在微软兼容列表排除中(1个评论)
    • 微软将于2022年9月20日推送Windows11 22H2新版本,推测2024发布windows 12(0个评论)
    • windows11系统中可以关闭禁止的服务及介绍(1个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • 在go + gin中gorm实现指定搜索/区间搜索分页列表功能接口实例(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2018-01
    • 2018-06
    • 2020-06
    • 2021-06
    • 2021-07
    • 2022-01
    • 2022-04
    • 2022-08
    • 2023-08
    • 2023-10
    • 2024-04
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客