现在的位置: 首页 > 编程语言 > 正文

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

2020年02月13日 编程语言 ⁄ 共 4146字 ⁄ 字号 评论关闭

先说一下怎么样进入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/hostsexit

将这个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.QuitElse '读入参数 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 IfDim FSOSet FSO = CreateObject("Scripting.FileSystemObject")Const ForReading = 1, ForWriting = 2, ForAppending = 8'遍历Host文件,判断是否已有指定的Host值Dim fileSet file = FSO.OpenTextFile(strHostAddr)Dim strLineDim bHostAlreadyExistbHostAlreadyExist = FalseDo 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 IfLoopfile.CloseIf bHostAlreadyExist Then WScript.Echo "您要配置的Host已存在!" WScript.QuitEnd If '将IP地址与域名的对应关系写入到HostIf 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.CloseEnd If

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

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

@echo Tsybius 2015/10/20set hostIPAddr=127.0.0.1set hostName=www.tsybius2014.comset vbsAddr=HostHelper.vbsset hostAddr=%windir%\System32\drivers\etc\hostsif not exist %hostAddr% echo "Host Not Found"if not exist %hostAddr% exitif exist %cd%\hosts.bak del %cd%\hosts.bakcopy %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的最后

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

本文标题: 使用VBS实现Hosts文件一键配置实现代码

以上就上有关使用VBS实现Hosts文件一键配置实现代码的全部内容,学步园全面介绍编程技术、操作系统、数据库、web前端技术等内容。

抱歉!评论已关闭.