域添加或删除hosts文件

    许多管理员在管理域的时候都会遇到修改hosts文件的实际需求,但是一个一个去客户端修改实在是又麻烦又很累的没效率的事情,通过一番查找与修改,发现可以通过vbs脚本在域中分发后进行hosts内记录的添加或修改。具体代码见下

添加hosts记录的vbs,将以下代码复制到记事本,文件后缀保存为vbs即可

Dim fso, objFile, rs,ws,fileString,i


Set fso = CreateObject("Scripting.

FileSystemObject")
Set objFile = fso.GetFile("C:\WINDOWS\ system32\drivers\etc\hosts")
Set rs = objFile.OpenAsTextStream(1,-2)
Set ws = objFile.OpenAsTextStream(8,-2)
fileString = rs.ReadAll()
subhost = "202.96.209.5"   (设置第一个dns ip)
i = InStr(fileString,subhost)
if i =0 Then
   ws.writeLine("#DNS")   (此处为解释,不需要复制:在hosts里做一个#解释)
   ws.writeLine("202.96.209.6 DNS")  (此处为解释,不需要复制:要添加的dns,根据实际需要修改,需要几个设置几行)
   ws.writeLine("203.208.39.104 lh1.ggpht.com")
   ws.writeLine("203.208.39.104 lh2.ggpht.com")
   ws.writeLine("203.208.39.104 lh3.ggpht.com")
   ws.writeLine("203.208.39.104 lh4.ggpht.com")
   ws.writeLine("203.208.39.104 lh5.ggpht.com")
   ws.writeLine("203.208.39.104 lh6.ggpht.com")
else
end if
rs.close()
ws.close()
 
 

删除hosts特定记录的vbs,将以下代码复制到记事本,文件后缀保存为vbs即可

    Sub DeleteFromHosts(lpKeywords)
    Const HostsPath="C:\Windows\

System32\Drivers\etc\Hosts"
    Dim fso
    Set fso=CreateObject("Scripting. FileSystemObject")
    tmp=fso.OpenTextFile( HostsPath,1).ReadAll
    For Each oStr In Split(tmp,vbNewLine)
            If oStr<>"" And InStr(oStr,lpKeywords)=0 Then NewHosts=NewHosts & oStr & vbNewLine
    Next
    fso.CreateTextFile(HostsPath, True).Write NewHosts
    Set fso=Nothing
    End Sub

    DeleteFromHosts "DNS"  ( 此处为解释,不需要复制:只要将双引号内的内容修改要删除的hosts内对应行的name 就可以,"203.208.39.104 lh1.ggpht.com",要删除这行就把 lh1.ggpht.com添上即可)
    DeleteFromHosts " lh1.ggpht.com"
    DeleteFromHosts " lh2.ggpht.com"
    DeleteFromHosts " lh3.ggpht.com"
    MsgBox "Hosts Fixed!"
 
做完之后,只需要在服务器中设置为一个开机脚本分发到域中即可。

你可能感兴趣的:(hosts,修改hosts,休闲,删除hosts,添加hosts记录)