使用VBS實現(xiàn)Hosts文件一鍵配置實現(xiàn)代碼
先說一下怎么樣進入hosts文件,Windows環(huán)境(我用的是一個32位的Win7)下hosts文件在計算機中的位置,在目錄%windir%\System32\drivers\etc\,文件名為hosts,沒有擴展名。不過相比每次都要點很多目錄才能找到hosts文件,我們可以通過執(zhí)行下面這個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下,就可以實現(xiàn)在命令行里或是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"
配置效果如下:
這個方法非常簡單,但是使用這個方法也存在缺點,即存在映射記錄可能被反復(fù)配置的情況。
因此我又試著寫了下面這個可以自動配置指定網(wǎng)址hosts的VBS腳本HostHelper.vbs,代碼如下:
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ' HostHelper Hosts文件配置工具 ' 作者:Tsybius2014 ' 時間:2015年10月20日 ' 描述:HostHelper 是一個Host文件配置工具,輸入為Host文件地址、IP地址、域名 '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' '強制顯式聲明模塊中的所有變量 Option Explicit '讀取參數(shù) Dim strHostAddr 'Host文件地址 Dim strIpAddr 'IP地址 Dim strName '主機名 Dim strOper '操作類型 cover:寫入 append:追加 If WScript.Arguments.Count <> 4 Then WScript.Echo "參數(shù)錯誤" 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:主機名 strOper = WScript.Arguments(3) '參數(shù)4:寫入策略 cover:覆蓋 append:追加 '覆蓋:排他性加入 '追加:在文件末尾添加IP地址與主機名對應(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 "主機名:" & 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地址與域名的對應(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
這個VBS腳本的功能,是傳入hosts文件地址、IP地址、主機名,并指定寫入策略(包括覆蓋、追加),執(zhí)行該腳本后會自動配置hosts文件。
為了更好地運行這個VBS腳本,我寫了一個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%\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文件進行了備份。
這個腳本的執(zhí)行效果如下:
進入hosts文件,可以看到映射已被寫入在hosts.txt的最后
說明:由于本文中的代碼只進行了簡單的測試,因此部分代碼可能存在健壯性不夠的問題,實際使用時應(yīng)謹慎使用。
相關(guān)文章
用vbs實現(xiàn)配置無人登錄計算機時使用的屏幕保護程序
用vbs實現(xiàn)配置無人登錄計算機時使用的屏幕保護程序...2007-03-03Stream、WshShell、WshUrlShortcut對象及Shell.Application的參數(shù)與使用
Stream、WshShell、WshUrlShortcut對象及Shell.Application的參數(shù)與使用...2007-03-03