gpt4 book ai didi

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

转载 作者:qq735679552 更新时间:2022-09-29 22:32:09 25 4
gpt4 key购买 nike

CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.

这篇CFSDN的博客文章使用VBS实现Hosts文件一键配置实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.

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

?
1
2
3
4
5
6
7
@ 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文件的最后追加一条记录:

?
1
2
3
4
5
6
@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"

配置效果如下:

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

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

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' 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批处理命令行来执行它,代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@ 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文件进行了备份.

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

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

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

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

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

原文链接:https://my.oschina.net/Tsybius2014/blog/527780 。

最后此篇关于使用VBS实现Hosts文件一键配置实现代码的文章就讲到这里了,如果你想了解更多关于使用VBS实现Hosts文件一键配置实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com