• 设为首页
  • 收藏本站
  • 积分充值
  • VIP赞助
  • 手机版
  • 微博
  • 微信
    微信公众号 添加方式:
    1:搜索微信号(888888
    2:扫描左侧二维码
  • 快捷导航
    福建二哥 门户 查看主题

    使用VBS实现Hosts文件一键配置实现代码

    发布者: 涵韵3588 | 发布时间: 2025-8-14 03:54| 查看数: 39| 评论数: 0|帖子模式

    先说一下怎么样进入hosts文件,Windows环境(我用的是一个32位的Win7)下hosts文件在计算机中的位置,在目录%windir%\System32\drivers\etc\,文件名为hosts,没有扩展名。不过相比每次都要点很多目录才能找到hosts文件,我们可以通过执行下面这个bat脚本直接用记事本打开hosts文件:
    1. @echo off
    2. if "%1" == "h" goto begin
    3. mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit
    4. :begin

    5. notepad %SystemRoot%/System32/drivers/etc/hosts
    6. exit
    复制代码
    将这个bat脚本取名为host.bat,放在C:\Windows\System32下,就可以实现在命令行里或是Win7的开始菜单中直接输入host命令打开hosts文件了。
    言归正传,下面我来说下如何自动向hosts文件后面插入记录。
    下面这个bat脚本,可以满足最简单的hosts配置,即在hosts文件的最后追加一条记录:
    1. @attrib -r "%windir%\System32\drivers\etc\hosts"
    2. @echo ###### Host配置 START >>"%windir%\System32\drivers\etc\hosts"  
    3. @echo 127.0.0.1 www.tsybius2014.com >>"%windir%\System32\drivers\etc\hosts"  
    4. @echo 127.0.0.1 www.tsybius2014.net >>"%windir%\System32\drivers\etc\hosts"
    5. @echo ###### Host配置 END >>"%windir%\System32\drivers\etc\hosts"  
    6. ::@attrib +r "%windir%\System32\drivers\etc\hosts"
    复制代码
    配置效果如下:

    这个方法非常简单,但是使用这个方法也存在缺点,即存在映射记录可能被反复配置的情况。
    因此我又试着写了下面这个可以自动配置指定网址hosts的VBS脚本HostHelper.vbs,代码如下:
    1. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    2. ' HostHelper Hosts文件配置工具
    3. ' 作者:Tsybius2014
    4. ' 时间:2015年10月20日
    5. ' 描述:HostHelper 是一个Host文件配置工具,输入为Host文件地址、IP地址、域名
    6. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

    7. '强制显式声明模块中的所有变量
    8. Option Explicit

    9. '读取参数
    10. Dim strHostAddr 'Host文件地址
    11. Dim strIpAddr  'IP地址
    12. Dim strName   '主机名
    13. Dim strOper   '操作类型 cover:写入 append:追加
    14. If WScript.Arguments.Count <> 4 Then
    15.   WScript.Echo "参数错误"
    16.   WScript.Quit
    17. Else
    18.   '读入参数
    19.   strHostAddr = WScript.Arguments(0) '参数1:Host文件地址
    20.   strIpAddr = WScript.Arguments(1)  '参数2:IP地址
    21.   strName = WScript.Arguments(2)   '参数3:主机名
    22.   strOper = WScript.Arguments(3)   '参数4:写入策略 cover:覆盖 append:追加
    23.   '覆盖:排他性加入
    24.   '追加:在文件末尾添加IP地址与主机名对应关系
    25.   '判断写入策略
    26.   Dim strOperName
    27.   If strOper = "cover" Then
    28.     strOperName = "覆盖"
    29.   ElseIf strOper = "append" Then
    30.     strOperName = "追加"
    31.   Else
    32.     WScript.Echo "非法的写入策略!"
    33.     WScript.Quit
    34.   End If
    35.   '展示输入信息
    36.   WScript.Echo "Host文件地址:" & strHostAddr
    37.   WScript.Echo "IP地址:" & strIpAddr
    38.   WScript.Echo "主机名:" & strName
    39.   WScript.Echo "写入策略:" & strOperName
    40.   WScript.Echo ""
    41. End If

    42. Dim FSO
    43. Set FSO = CreateObject("Scripting.FileSystemObject")
    44. Const ForReading = 1, ForWriting = 2, ForAppending = 8

    45. '遍历Host文件,判断是否已有指定的Host值
    46. Dim file
    47. Set file = FSO.OpenTextFile(strHostAddr)
    48. Dim strLine
    49. Dim bHostAlreadyExist
    50. bHostAlreadyExist = False
    51. Do While file.AtEndOfStream <> True
    52.   strLine = LTrim(file.ReadLine)
    53.   If Not Left(strLine, 1) = "#" Then
    54.     Dim Array
    55.     Array = Split(strLine, " ", -1, 1)
    56.     If UBound(Array) >= 1 Then
    57.       'IP一样且域名一样,则认为要配置的Host已存在
    58.       If Array(0) = strIpAddr And Array(1) = strName Then
    59.         bHostAlreadyExist = True
    60.         Exit Do
    61.       End If
    62.     Else
    63.     End If
    64.     'WScript.Echo strLine
    65.   Else
    66.   End If
    67. Loop
    68. file.Close

    69. If bHostAlreadyExist Then
    70.   WScript.Echo "您要配置的Host已存在!"
    71.   WScript.Quit
    72. End If

    73. '将IP地址与域名的对应关系写入到Host
    74. If strOper = "cover" Then

    75.   '写入策略:覆盖
    76.   Dim fileRead
    77.   Set fileRead = FSO.OpenTextFile(strHostAddr)
    78.   Dim strContent
    79.   strContent = fileRead.ReadAll()
    80.   fileRead.Close
    81.   Dim ArrayLine
    82.   ArrayLine = Split(strContent, vbCrlf, -1, 1)
    83.   Dim i
    84.   Dim strArrayEachLine
    85.   For i = 0 To UBound(ArrayLine)
    86.     ArrayLine(i) = Trim(ArrayLine(i))
    87.     If Not Left(ArrayLine(i), 1) = "#" Then
    88.       strArrayEachLine = Split(ArrayLine(i), " ", -1, 1)
    89.       If UBound(strArrayEachLine) >= 1 Then
    90.         If strArrayEachLine(1) = strName Then
    91.           ArrayLine(i) = "#" & ArrayLine(i)
    92.         End If
    93.       End If
    94.     End If
    95.   Next
    96.   strContent = Join(ArrayLine, vbCrlf)
    97.   strContent = strContent & vbCrlf & strIpAddr & " " & strName
    98.   Dim fileCover
    99.   Set fileCover = FSO.OpenTextFile(strHostAddr, ForWriting, False)
    100.   fileCover.Write strContent
    101.   fileCover.Close
    102.   WScript.Echo "覆盖完毕"
    103.   
    104. ElseIf strOper = "append" Then

    105.   '写入策略:追加
    106.   Dim fileAppend
    107.   Set fileAppend = FSO.OpenTextFile(strHostAddr, ForAppending, False)
    108.   fileAppend.WriteLine
    109.   fileAppend.WriteLine strIpAddr & " " & strName
    110.   WScript.Echo "追加完毕"
    111.   fileAppend.Close

    112. End If
    复制代码
    这个VBS脚本的功能,是传入hosts文件地址、IP地址、主机名,并指定写入策略(包括覆盖、追加),执行该脚本后会自动配置hosts文件。
    为了更好地运行这个VBS脚本,我写了一个bat批处理命令行来执行它,代码如下:
    1. @echo Tsybius 2015/10/20

    2. set hostIPAddr=127.0.0.1
    3. set hostName=www.tsybius2014.com
    4. set vbsAddr=HostHelper.vbs
    5. set hostAddr=%windir%\System32\drivers\etc\hosts

    6. if not exist %hostAddr% echo "Host Not Found"
    7. if not exist %hostAddr% exit
    8. if exist %cd%\hosts.bak del %cd%\hosts.bak
    9. copy %hostAddr% %cd%\hosts.bak

    10. @attrib -r %hostAddr%
    11. cscript %vbsaddr% %hostAddr% hostIPAddr hostName append
    12. ::@attrib +r %hostAddr%

    13. @pause
    复制代码
    这个脚本试图向hosts文件的最后追加一条记录,域名为www.tsybius2014.com,IP为127.0.0.1,写入策略为追加,并且在写入前先对hosts文件进行了备份。
    这个脚本的执行效果如下:

    进入hosts文件,可以看到映射已被写入在hosts.txt的最后

    说明:由于本文中的代码只进行了简单的测试,因此部分代码可能存在健壮性不够的问题,实际使用时应谨慎使用。

    来源:互联网
    免责声明:如果侵犯了您的权益,请联系站长(1277306191@qq.com),我们会及时删除侵权内容,谢谢合作!

    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    ×

    最新评论

    QQ Archiver 手机版 小黑屋 福建二哥 ( 闽ICP备2022004717号|闽公网安备35052402000345号 )

    Powered by Discuz! X3.5 © 2001-2023

    快速回复 返回顶部 返回列表