佳木斯湛栽影视文化发展公司

主頁 > 知識(shí)庫 > 使用VBS實(shí)現(xiàn)Hosts文件一鍵配置實(shí)現(xiàn)代碼

使用VBS實(shí)現(xiàn)Hosts文件一鍵配置實(shí)現(xiàn)代碼

熱門標(biāo)簽:服務(wù)器配置 呼叫中心市場需求 智能手機(jī) 檢查注冊表項(xiàng) 網(wǎng)站文章發(fā)布 鐵路電話系統(tǒng) 美圖手機(jī) 銀行業(yè)務(wù)

先說一下怎么樣進(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ī)名對應(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地址與域名的對應(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,寫入策略為追加,并且在寫入前先對hosts文件進(jìn)行了備份。

這個(gè)腳本的執(zhí)行效果如下:

進(jìn)入hosts文件,可以看到映射已被寫入在hosts.txt的最后

說明:由于本文中的代碼只進(jìn)行了簡單的測試,因此部分代碼可能存在健壯性不夠的問題,實(shí)際使用時(shí)應(yīng)謹(jǐn)慎使用。

 
 

標(biāo)簽:沈陽 滄州 紅河 長治 上海 河南 新疆 樂山

巨人網(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)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    永登县| 宁阳县| 壶关县| 南涧| 同江市| 五台县| 密云县| 松滋市| 怀集县| 湘潭市| 涡阳县| 贡山| 靖安县| 江山市| 遵义县| 富川| 石嘴山市| 毕节市| 西乌| 墨玉县| 兴国县| 隆化县| 射洪县| 如皋市| 微山县| 化州市| 石林| 甘洛县| 军事| 灯塔市| 杨浦区| 泾阳县| 通许县| 独山县| 科技| 铁力市| 兴业县| 平江县| 始兴县| 横山县| 方山县|