当前位置: 移动技术网 > IT编程>脚本编程>VBScript > 使用VBS实现Hosts文件一键配置实现代码

使用VBS实现Hosts文件一键配置实现代码

2020年03月09日  | 移动技术网IT编程  | 我要评论

上海 电影,春暖花开公告,六年级上册科学教案

先说一下怎么样进入hosts文件,windows环境(我用的是一个32位的win7)下hosts文件在计算机中的位置,在目录%windir%\system32\drivers\etc\,文件名为hosts,没有扩展名。不过相比每次都要点很多目录才能找到hosts文件,我们可以通过执行下面这个bat脚本直接用记事本打开hosts文件:

@echo off 
if "%1" == "h" goto begin 
mshta vbscript:createobject("wscript.shell").run("%~nx0 h",0)(window.close)&&exit 
:begin 

notepad %systemroot%/system32/drivers/etc/hosts
exit

将这个bat脚本取名为host.bat,放在c:\windows\system32下,就可以实现在命令行里或是win7的开始菜单中直接输入host命令打开hosts文件了。

言归正传,下面我来说下如何自动向hosts文件后面插入记录。

下面这个bat脚本,可以满足最简单的hosts配置,即在hosts文件的最后追加一条记录:

@attrib -r "%windir%\system32\drivers\etc\hosts" 
@echo ###### host配置 start >>"%windir%\system32\drivers\etc\hosts"  
@echo 127.0.0.1 www.tsybius2014.com >>"%windir%\system32\drivers\etc\hosts"  
@echo 127.0.0.1 www.tsybius2014.net >>"%windir%\system32\drivers\etc\hosts" 
@echo ###### host配置 end >>"%windir%\system32\drivers\etc\hosts"  
::@attrib +r "%windir%\system32\drivers\etc\hosts"

配置效果如下:

这个方法非常简单,但是使用这个方法也存在缺点,即存在映射记录可能被反复配置的情况。

因此我又试着写了下面这个可以自动配置指定网址hosts的vbs脚本hosthelper.vbs,代码如下:

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' hosthelper hosts文件配置工具
' 作者:tsybius2014
' 时间:2015年10月20日
' 描述:hosthelper 是一个host文件配置工具,输入为host文件地址、ip地址、域名
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

'强制显式声明模块中的所有变量
option explicit

'读取参数
dim strhostaddr 'host文件地址
dim stripaddr  'ip地址
dim strname   '主机名
dim stroper   '操作类型 cover:写入 append:追加
if wscript.arguments.count <> 4 then
  wscript.echo "参数错误"
  wscript.quit
else 
  '读入参数
  strhostaddr = wscript.arguments(0) '参数1:host文件地址
  stripaddr = wscript.arguments(1)  '参数2:ip地址
  strname = wscript.arguments(2)   '参数3:主机名
  stroper = wscript.arguments(3)   '参数4:写入策略 cover:覆盖 append:追加
  '覆盖:排他性加入
  '追加:在文件末尾添加ip地址与主机名对应关系
  '判断写入策略
  dim stropername
  if stroper = "cover" then 
    stropername = "覆盖"
  elseif stroper = "append" then
    stropername = "追加"
  else
    wscript.echo "非法的写入策略!"
    wscript.quit
  end if
  '展示输入信息
  wscript.echo "host文件地址:" & strhostaddr
  wscript.echo "ip地址:" & stripaddr
  wscript.echo "主机名:" & strname
  wscript.echo "写入策略:" & stropername
  wscript.echo ""
end if

dim fso
set fso = createobject("scripting.filesystemobject")
const forreading = 1, forwriting = 2, forappending = 8

'遍历host文件,判断是否已有指定的host值
dim file
set file = fso.opentextfile(strhostaddr)
dim strline
dim bhostalreadyexist
bhostalreadyexist = false
do while file.atendofstream <> true
  strline = ltrim(file.readline)
  if not left(strline, 1) = "#" then
    dim array
    array = split(strline, " ", -1, 1)
    if ubound(array) >= 1 then
      'ip一样且域名一样,则认为要配置的host已存在
      if array(0) = stripaddr and array(1) = strname then
        bhostalreadyexist = true
        exit do
      end if
    else 
    end if 
    'wscript.echo strline
  else 
  end if
loop
file.close

if bhostalreadyexist then
  wscript.echo "您要配置的host已存在!"
  wscript.quit
end if 

'将ip地址与域名的对应关系写入到host
if stroper = "cover" then

  '写入策略:覆盖
  dim fileread
  set fileread = fso.opentextfile(strhostaddr)
  dim strcontent
  strcontent = fileread.readall()
  fileread.close
  dim arrayline
  arrayline = split(strcontent, vbcrlf, -1, 1)
  dim i
  dim strarrayeachline
  for i = 0 to ubound(arrayline)
    arrayline(i) = trim(arrayline(i))
    if not left(arrayline(i), 1) = "#" then
      strarrayeachline = split(arrayline(i), " ", -1, 1)
      if ubound(strarrayeachline) >= 1 then
        if strarrayeachline(1) = strname then
          arrayline(i) = "#" & arrayline(i)
        end if
      end if
    end if
  next
  strcontent = join(arrayline, vbcrlf)
  strcontent = strcontent & vbcrlf & stripaddr & " " & strname
  dim filecover
  set filecover = fso.opentextfile(strhostaddr, forwriting, false)
  filecover.write strcontent
  filecover.close
  wscript.echo "覆盖完毕"
  
elseif stroper = "append" then

  '写入策略:追加
  dim fileappend
  set fileappend = fso.opentextfile(strhostaddr, forappending, false)
  fileappend.writeline
  fileappend.writeline stripaddr & " " & strname
  wscript.echo "追加完毕"
  fileappend.close

end if

这个vbs脚本的功能,是传入hosts文件地址、ip地址、主机名,并指定写入策略(包括覆盖、追加),执行该脚本后会自动配置hosts文件。

为了更好地运行这个vbs脚本,我写了一个bat批处理命令行来执行它,代码如下:

@echo tsybius 2015/10/20

set hostipaddr=127.0.0.1
set hostname=www.tsybius2014.com
set vbsaddr=hosthelper.vbs
set hostaddr=%windir%\system32\drivers\etc\hosts

if not exist %hostaddr% echo "host not found"
if not exist %hostaddr% exit
if exist %cd%\hosts.bak del %cd%\hosts.bak
copy %hostaddr% %cd%\hosts.bak

@attrib -r %hostaddr%
cscript %vbsaddr% %hostaddr% hostipaddr hostname append
::@attrib +r %hostaddr%

@pause

这个脚本试图向hosts文件的最后追加一条记录,域名为www.tsybius2014.com,ip为127.0.0.1,写入策略为追加,并且在写入前先对hosts文件进行了备份。

这个脚本的执行效果如下:

进入hosts文件,可以看到映射已被写入在hosts.txt的最后

说明:由于本文中的代码只进行了简单的测试,因此部分代码可能存在健壮性不够的问题,实际使用时应谨慎使用。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网