VBScript(Microsoft Visual Basic Script Edition).,微软公司可视化BASIC脚本版. 正如其字面所透露的信息, VBS(VBScript的进一步简写)是基于Visual Basic的脚本语言。进一步解释Microsoft Visual Basic是微软公司出品的一套可视化编程工具, 语法基于Basic.脚本语言, 不用编译成二进制文件, 直接由宿主(host)解释源代码并执行, 简单点说就是写的程序不需要编译成.exe文件, 而是直接给用户发送.vbs的源程序, 用户就可以执行。
host文件地址C:\Windows\System32\drivers\etc目录下,假设原host文件中增加内容如下:
xxx.xxx.xxx.xxx www.yoodb.com xxx.xxx.xxx.xxx images.yoodb.com xxx.xxx.xxx.xxx www.yoodb.com
不需要上述内容,直接运行vbs代码,具体如下:
'执行该脚本需本地管理员权限 Const ForReading = 1, ForWriting = 2, ForAppending = 8, ReadOnly = 1 Set fso = CreateObject("Scripting.FileSystemObject") Set WshShell=CreateObject("WScript.Shell") 'WinDir 为windows安装目录 WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%") '设定host 文件目录 HostsFile = WinDir & "\System32\Drivers\etc\HOSTS" '检查host文件是否为只读,如为只读,则修改文件属性 Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.GetFile(HostsFile) If objFile.Attributes And ReadOnly Then objFile.Attributes = objFile.Attributes Xor ReadOnly End If Set objFSO = CreateObject("Scripting.FileSystemObject") Set objFile = objFSO.OpenTextFile(HostsFile, ForReading,true) '检查host文件里面是否已经更改,如果更改过,则不再执行脚本 hostfileline="" Do Until objFile.AtEndOfStream strline = objfile.ReadLine If InStr (strline, "xxx.xxx.xxx.xxx") <> 0 Or (InStr (strline, "xxx.xxx.xxx.xxx"))<>0 Or (InStr (strline, "xxx.xxx.xxx.xxx")) Then strline="" End If hostfileline=hostfileline+vbCrLf+strline Loop WScript.Echo hostfileline objFile.Close '修改host文件 Set filetxt = fso.OpenTextFile(HostsFile, ForWriting ) filetxt.Write hostfileline filetxt.Close WScript.Quit