使用vbs删除host文件域址内容
程序员文章站
2022-03-21 16:28:01
要求:原先host里面已增加以下3行
202.102.101.105 intranet.corp
202.102.101.107 mail.intranet.corp...
要求:原先host里面已增加以下3行
202.102.101.105 intranet.corp
202.102.101.107 mail.intranet.corp
202.102.101.108 sip.intranet.corp
之后不需要此3行内容,所以运行以上vbs代码,来删除host文件此3行
'该脚本要求执行用户有本地管理员权限 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, "202.102.101.105") <> 0 or (instr (strline, "202.102.101.107"))<>0 or (instr (strline, "202.102.101.108")) 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
下一篇: Python 键盘事件详解