先說一下怎么樣進(jìn)入hosts文件,Windows環(huán)境(我用的是一個(gè)32位的Win7)下hosts文件在計(jì)算機(jī)中的位置,在目錄%windir%System32driversetc,文件名為hosts,沒有擴(kuò)展名。不過相比每次都要點(diǎn)很多目錄才能找到hosts文件,我們可以通過執(zhí)行下面這個(gè)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
將這個(gè)bat腳本取名為host.bat,放在C:WindowsSystem32下,就可以實(shí)現(xiàn)在命令行里或是Win7的開始菜單中直接輸入host命令打開hosts文件了。
言歸正傳,下面我來說下如何自動(dòng)向hosts文件后面插入記錄。
下面這個(gè)bat腳本,可以滿足最簡單的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"
配置效果如下:
這個(gè)方法非常簡單,但是使用這個(gè)方法也存在缺點(diǎn),即存在映射記錄可能被反復(fù)配置的情況。
因此我又試著寫了下面這個(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:寫入 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:寫入策略 cover:覆蓋 append:追加 '覆蓋:排他性加入 '追加:在文件末尾添加IP地址與主機(jī)名對(duì)應(yīng)關(guān)系 '判斷寫入策略 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 "主機(jī)名:" 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一樣且域名一樣,則認(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)系寫入到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
這個(gè)VBS腳本的功能,是傳入hosts文件地址、IP地址、主機(jī)名,并指定寫入策略(包括覆蓋、追加),執(zhí)行該腳本后會(huì)自動(dòng)配置hosts文件。
為了更好地運(yùn)行這個(gè)VBS腳本,我寫了一個(gè)bat批處理命令行來執(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,寫入策略為追加,并且在寫入前先對(duì)hosts文件進(jìn)行了備份。
這個(gè)腳本的執(zhí)行效果如下:
進(jìn)入hosts文件,可以看到映射已被寫入在hosts.txt的最后
說明:由于本文中的代碼只進(jìn)行了簡單的測試,因此部分代碼可能存在健壯性不夠的問題,實(shí)際使用時(shí)應(yīng)謹(jǐn)慎使用。
標(biāo)簽:銅川 文山 鶴壁 湖州 西安 四平 邢臺(tái)
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用VBS實(shí)現(xiàn)Hosts文件一鍵配置實(shí)現(xiàn)代碼》,本文關(guān)鍵詞 使用,VBS,實(shí)現(xiàn),Hosts,文件,一鍵,配置,代碼,先,說,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。