五月综合激情婷婷六月,日韩欧美国产一区不卡,他扒开我内裤强吻我下面视频 ,无套内射无矿码免费看黄,天天躁,日日躁,狠狠躁

新聞動(dòng)態(tài)

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

發(fā)布日期:2021-12-27 08:25 | 文章來(lái)源:源碼中國(guó)

先說(shuō)一下怎么樣進(jìn)入hosts文件,Windows環(huán)境(我用的是一個(gè)32位的Win7)下hosts文件在計(jì)算機(jī)中的位置,在目錄%windir%\System32\drivers\etc\,文件名為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:\Windows\System32下,就可以實(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%\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"

配置效果如下:

這個(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'操作類(lèi)型 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%\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

這個(gè)腳本試圖向hosts文件的最后追加一條記錄,域名為www.tsybius2014.com,IP為127.0.0.1,寫(xiě)入策略為追加,并且在寫(xiě)入前先對(duì)hosts文件進(jìn)行了備份。

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

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

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

版權(quán)聲明:本站文章來(lái)源標(biāo)注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請(qǐng)保持原文完整并注明來(lái)源及原文鏈接。禁止復(fù)制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務(wù)器上建立鏡像,否則將依法追究法律責(zé)任。本站部分內(nèi)容來(lái)源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來(lái),僅供學(xué)習(xí)參考,不代表本站立場(chǎng),如有內(nèi)容涉嫌侵權(quán),請(qǐng)聯(lián)系alex-e#qq.com處理。

相關(guān)文章

實(shí)時(shí)開(kāi)通

自選配置、實(shí)時(shí)開(kāi)通

免備案

全球線路精選!

全天候客戶(hù)服務(wù)

7x24全年不間斷在線

專(zhuān)屬顧問(wèn)服務(wù)

1對(duì)1客戶(hù)咨詢(xún)顧問(wèn)

在線
客服

在線客服:7*24小時(shí)在線

客服
熱線

400-630-3752
7*24小時(shí)客服服務(wù)熱線

關(guān)注
微信

關(guān)注官方微信
頂部