原创

vbs 删除 host 文件中部分IP于域名地址

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
~阅读全文-人机检测~

微信公众号“Java精选”(w_z90110),专注Java技术干货分享!让你从此路人变大神!回复关键词领取资料:如Mysql、Hadoop、Dubbo、Spring Boot等,免费领取视频教程、资料文档和项目源码。微信搜索小程序“Java精选面试题”,内涵3000+道Java面试题!

涵盖:互联网那些事、算法与数据结构、SpringMVC、Spring boot、Spring Cloud、ElasticSearch、Linux、Mysql、Oracle等

评论

分享:

支付宝

微信