LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

使用VBS脚本实现的Hosts文件一键配置

时间:2015-11-09 21:40来源:Tsybius2014 编辑:Tsybius2014 点击:
hosts文件存储了IP地址与域名的映射。因为有的时候需要经常性地配置hosts文件,因此这段时间我琢磨了一套傻瓜化的hosts文件配置方案,记录如下。 先说一下怎么样进入hosts文件,Windo
hosts文件存储了IP地址与域名的映射。因为有的时候需要经常性地配置hosts文件,因此这段时间我琢磨了一套傻瓜化的hosts文件配置方案,记录如下。

先说一下怎么样进入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的最后

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

转载请保留固定链接: https://linuxeye.com/program/2831.html

------分隔线----------------------------
标签:VBS
栏目列表
推荐内容