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

所属分类: 脚本专栏 / vbs 阅读数: 1457
收藏 0 赞 0 分享

先说一下怎么样进入hosts文件,Windows环境(我用的是一个32位的Win7)下hosts文件在计算机中的位置,在目录%windir%\System32\drivers\etc\,文件名为hosts,没有扩展名。不过相比每次都要点很多目录才能找到hosts文件,我们可以通过执行下面这个bat脚本直接用记事本打开hosts文件:

@echo off 
if "%1" == "h" goto begin 
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin 

notepad %SystemRoot%/System32/drivers/etc/hosts
exit

将这个bat脚本取名为host.bat,放在C:\Windows\System32下,就可以实现在命令行里或是Win7的开始菜单中直接输入host命令打开hosts文件了。

言归正传,下面我来说下如何自动向hosts文件后面插入记录。

下面这个bat脚本,可以满足最简单的hosts配置,即在hosts文件的最后追加一条记录:

@attrib -r "%windir%\System32\drivers\etc\hosts" 
@echo ###### Host配置 START >>"%windir%\System32\drivers\etc\hosts"  
@echo 127.0.0.1 www.tsybius2014.com >>"%windir%\System32\drivers\etc\hosts"  
@echo 127.0.0.1 www.tsybius2014.net >>"%windir%\System32\drivers\etc\hosts" 
@echo ###### Host配置 END >>"%windir%\System32\drivers\etc\hosts"  
::@attrib +r "%windir%\System32\drivers\etc\hosts"

配置效果如下:

这个方法非常简单,但是使用这个方法也存在缺点,即存在映射记录可能被反复配置的情况。

因此我又试着写了下面这个可以自动配置指定网址hosts的VBS脚本HostHelper.vbs,代码如下:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' HostHelper Hosts文件配置工具
' 作者:Tsybius2014
' 时间:2015年10月20日
' 描述:HostHelper 是一个Host文件配置工具,输入为Host文件地址、IP地址、域名
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

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

'读取参数
Dim strHostAddr 'Host文件地址
Dim strIpAddr  'IP地址
Dim strName   '主机名
Dim strOper   '操作类型 cover:写入 append:追加
If WScript.Arguments.Count <> 4 Then
  WScript.Echo "参数错误"
  WScript.Quit
Else 
  '读入参数
  strHostAddr = WScript.Arguments(0) '参数1:Host文件地址
  strIpAddr = WScript.Arguments(1)  '参数2:IP地址
  strName = WScript.Arguments(2)   '参数3:主机名
  strOper = WScript.Arguments(3)   '参数4:写入策略 cover:覆盖 append:追加
  '覆盖:排他性加入
  '追加:在文件末尾添加IP地址与主机名对应关系
  '判断写入策略
  Dim strOperName
  If strOper = "cover" Then 
    strOperName = "覆盖"
  ElseIf strOper = "append" Then
    strOperName = "追加"
  Else
    WScript.Echo "非法的写入策略!"
    WScript.Quit
  End If
  '展示输入信息
  WScript.Echo "Host文件地址:" & strHostAddr
  WScript.Echo "IP地址:" & strIpAddr
  WScript.Echo "主机名:" & strName
  WScript.Echo "写入策略:" & strOperName
  WScript.Echo ""
End If

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

'遍历Host文件,判断是否已有指定的Host值
Dim file
Set file = FSO.OpenTextFile(strHostAddr)
Dim strLine
Dim bHostAlreadyExist
bHostAlreadyExist = False
Do While file.AtEndOfStream <> True
  strLine = LTrim(file.ReadLine)
  If Not Left(strLine, 1) = "#" Then
    Dim Array
    Array = Split(strLine, " ", -1, 1)
    If UBound(Array) >= 1 Then
      'IP一样且域名一样,则认为要配置的Host已存在
      If Array(0) = strIpAddr And Array(1) = strName Then
        bHostAlreadyExist = True
        Exit Do
      End If
    Else 
    End If 
    'WScript.Echo strLine
  Else 
  End If
Loop
file.Close

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

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

  '写入策略:覆盖
  Dim fileRead
  Set fileRead = FSO.OpenTextFile(strHostAddr)
  Dim strContent
  strContent = fileRead.ReadAll()
  fileRead.Close
  Dim ArrayLine
  ArrayLine = Split(strContent, vbCrlf, -1, 1)
  Dim i
  Dim strArrayEachLine
  For i = 0 To UBound(ArrayLine)
    ArrayLine(i) = Trim(ArrayLine(i))
    If Not Left(ArrayLine(i), 1) = "#" Then
      strArrayEachLine = Split(ArrayLine(i), " ", -1, 1)
      If UBound(strArrayEachLine) >= 1 Then
        If strArrayEachLine(1) = strName Then
          ArrayLine(i) = "#" & ArrayLine(i)
        End If
      End If
    End If
  Next
  strContent = Join(ArrayLine, vbCrlf)
  strContent = strContent & vbCrlf & strIpAddr & " " & strName
  Dim fileCover
  Set fileCover = FSO.OpenTextFile(strHostAddr, ForWriting, False)
  fileCover.Write strContent
  fileCover.Close
  WScript.Echo "覆盖完毕"
  
ElseIf strOper = "append" Then

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

End If

这个VBS脚本的功能,是传入hosts文件地址、IP地址、主机名,并指定写入策略(包括覆盖、追加),执行该脚本后会自动配置hosts文件。

为了更好地运行这个VBS脚本,我写了一个bat批处理命令行来执行它,代码如下:

@echo Tsybius 2015/10/20

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

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

@attrib -r %hostAddr%
cscript %vbsaddr% %hostAddr% hostIPAddr hostName append
::@attrib +r %hostAddr%

@pause

这个脚本试图向hosts文件的最后追加一条记录,域名为www.tsybius2014.com,IP为127.0.0.1,写入策略为追加,并且在写入前先对hosts文件进行了备份。

这个脚本的执行效果如下:

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

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

更多精彩内容其他人还在看

vbscript禁用 启用fso的方法

启用:regsvr32 scrrun.dll 禁用:regsvr32 /u scrrun.dll
收藏 0 赞 0 分享

getSQLinfo.vbs 获得SQL数据/日志空间使用情况的脚本

这个脚本可以获取SQL数据/日志的空间使用情况方便及时了解sql使用空间情况
收藏 0 赞 0 分享

高手必看的vbs的至尊境界

vbs高手总结出来的,看来我们真忽略了他的真正强大的地方
收藏 0 赞 0 分享

vbs 获取radmin注册表中的信息

用这个脚本真的很方便,轻松的获取radmin注册表中的信息 ,想想以前我们都是用cmd下导出注册表信息
收藏 0 赞 0 分享

vbs解答一道初中数学题i,x,y

vbs解答 一道初中数学题 i+100=x^2 i+168=y^2 求 i,x,y
收藏 0 赞 0 分享

vbs之自动安装驱动程序

目前各类万能驱动程序包在网络上屡见发布,在使用这些程序包的同时,我们不仅会问:为什么这些程序包中的驱动程序可以在安装新硬件之后自动安装呢?
收藏 0 赞 0 分享

vbs Windows系统改变或修改网卡的MAC地址的脚本与软件

这个文件比程序本身还大,感觉不爽,于是本人的VBS版MAC修改代码便诞生了,在使用过程中如果出现不能上网的情况得返回一下网卡驱动(有些机器比较特别)
收藏 0 赞 0 分享

让IIS建立的站点默认是.net 2.0的,而不是.net 1.1的代码

让IIS建立的站点默认是.net 2.0的,而不是.net 1.1的,没有使用WMI,所以在操作前先得停止IIS相关服务
收藏 0 赞 0 分享

VBS利用SendKeys输入中文字符的方法

Author:Trajon.BWL今天在网上随便闲逛,逛到一个帖子,这位迷茫的朋友想知道该怎么使用SendKeys的vbs方法来输入中文字符
收藏 0 赞 0 分享

vbs加administrator用户的代码

使用ADSI的Winnt对象,Windows2000后面的系统都属于NT系列
收藏 0 赞 0 分享
查看更多