当前位置: 代码网 > it编程>前端脚本>其它脚本 > 使用vbs删除host文件域址内容

使用vbs删除host文件域址内容

2024年05月19日 其它脚本 我要评论
要求:原先host里面已增加以下3行202.102.101.105 intranet.corp202.102.101.107 mail.intranet.corp202.102.101.108 sip

要求:原先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

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com