先說(shuō)一下怎么樣進(jìn)入hosts文件,Windows環(huán)境(我用的是一個(gè)32位的Win7)下hosts文件在計(jì)算機(jī)中的位置,在目錄%windir%System32driversetc,文件名為hosts,沒(méi)有擴(kuò)展名。不過(guò)相比每次都要點(diǎn)很多目錄才能找到hosts文件,我們可以通過(guò)執(zhí)行下面這個(gè)bat腳本直接用記事本打開(kāi)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
將這個(gè)bat腳本取名為host.bat,放在C:WindowsSystem32下,就可以實(shí)現(xiàn)在命令行里或是Win7的開(kāi)始菜單中直接輸入host命令打開(kāi)hosts文件了。
言歸正傳,下面我來(lái)說(shuō)下如何自動(dòng)向hosts文件后面插入記錄。
下面這個(gè)bat腳本,可以滿足最簡(jiǎn)單的hosts配置,即在hosts文件的最后追加一條記錄:
@attrib -r "%windir%System32driversetchosts"
@echo ###### Host配置 START >>"%windir%System32driversetchosts"
@echo 127.0.0.1 www.tsybius2014.com >>"%windir%System32driversetchosts"
@echo 127.0.0.1 www.tsybius2014.net >>"%windir%System32driversetchosts"
@echo ###### Host配置 END >>"%windir%System32driversetchosts"
::@attrib +r "%windir%System32driversetchosts"
配置效果如下:
![](/d/20211017/280bcf57020b94314866dbd49a337f90.gif)
這個(gè)方法非常簡(jiǎn)單,但是使用這個(gè)方法也存在缺點(diǎn),即存在映射記錄可能被反復(fù)配置的情況。
因此我又試著寫(xiě)了下面這個(gè)可以自動(dòng)配置指定網(wǎng)址hosts的VBS腳本HostHelper.vbs,代碼如下:
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' HostHelper Hosts文件配置工具
' 作者:Tsybius2014
' 時(shí)間:2015年10月20日
' 描述:HostHelper 是一個(gè)Host文件配置工具,輸入為Host文件地址、IP地址、域名
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'強(qiáng)制顯式聲明模塊中的所有變量
Option Explicit
'讀取參數(shù)
Dim strHostAddr 'Host文件地址
Dim strIpAddr 'IP地址
Dim strName '主機(jī)名
Dim strOper '操作類型 cover:寫(xiě)入 append:追加
If WScript.Arguments.Count > 4 Then
WScript.Echo "參數(shù)錯(cuò)誤"
WScript.Quit
Else
'讀入?yún)?shù)
strHostAddr = WScript.Arguments(0) '參數(shù)1:Host文件地址
strIpAddr = WScript.Arguments(1) '參數(shù)2:IP地址
strName = WScript.Arguments(2) '參數(shù)3:主機(jī)名
strOper = WScript.Arguments(3) '參數(shù)4:寫(xiě)入策略 cover:覆蓋 append:追加
'覆蓋:排他性加入
'追加:在文件末尾添加IP地址與主機(jī)名對(duì)應(yīng)關(guān)系
'判斷寫(xiě)入策略
Dim strOperName
If strOper = "cover" Then
strOperName = "覆蓋"
ElseIf strOper = "append" Then
strOperName = "追加"
Else
WScript.Echo "非法的寫(xiě)入策略!"
WScript.Quit
End If
'展示輸入信息
WScript.Echo "Host文件地址:" strHostAddr
WScript.Echo "IP地址:" strIpAddr
WScript.Echo "主機(jī)名:" strName
WScript.Echo "寫(xiě)入策略:" 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一樣且域名一樣,則認(rèn)為要配置的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地址與域名的對(duì)應(yīng)關(guān)系寫(xiě)入到Host
If strOper = "cover" Then
'寫(xiě)入策略:覆蓋
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
'寫(xiě)入策略:追加
Dim fileAppend
Set fileAppend = FSO.OpenTextFile(strHostAddr, ForAppending, False)
fileAppend.WriteLine
fileAppend.WriteLine strIpAddr " " strName
WScript.Echo "追加完畢"
fileAppend.Close
End If
這個(gè)VBS腳本的功能,是傳入hosts文件地址、IP地址、主機(jī)名,并指定寫(xiě)入策略(包括覆蓋、追加),執(zhí)行該腳本后會(huì)自動(dòng)配置hosts文件。
為了更好地運(yùn)行這個(gè)VBS腳本,我寫(xiě)了一個(gè)bat批處理命令行來(lái)執(zhí)行它,代碼如下:
@echo Tsybius 2015/10/20
set hostIPAddr=127.0.0.1
set hostName=www.tsybius2014.com
set vbsAddr=HostHelper.vbs
set hostAddr=%windir%System32driversetchosts
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
這個(gè)腳本試圖向hosts文件的最后追加一條記錄,域名為www.tsybius2014.com,IP為127.0.0.1,寫(xiě)入策略為追加,并且在寫(xiě)入前先對(duì)hosts文件進(jìn)行了備份。
這個(gè)腳本的執(zhí)行效果如下:
![](/d/20211017/ad887c415f4a644144fbed50d39dd0d2.gif)
進(jìn)入hosts文件,可以看到映射已被寫(xiě)入在hosts.txt的最后
![](/d/20211017/aa528805aa8a0fa94933f406901c7f82.gif)
說(shuō)明:由于本文中的代碼只進(jìn)行了簡(jiǎn)單的測(cè)試,因此部分代碼可能存在健壯性不夠的問(wèn)題,實(shí)際使用時(shí)應(yīng)謹(jǐn)慎使用。