- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
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"
|
配置效果如下:
这个方法非常简单,但是使用这个方法也存在缺点,即存在映射记录可能被反复配置的情况.
因此我又试着写了下面这个可以自动配置指定网址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文件进行了备份.
这个脚本的执行效果如下:
进入hosts文件,可以看到映射已被写入在hosts.txt的最后 。
说明:由于本文中的代码只进行了简单的测试,因此部分代码可能存在健壮性不够的问题,实际使用时应谨慎使用.
原文链接:https://my.oschina.net/Tsybius2014/blog/527780 。
最后此篇关于使用VBS实现Hosts文件一键配置实现代码的文章就讲到这里了,如果你想了解更多关于使用VBS实现Hosts文件一键配置实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我尝试理解[c代码 -> 汇编]代码 void node::Check( data & _data1, vector& _data2) { -> push ebp -> mov ebp,esp ->
我需要在当前表单(代码)的上下文中运行文本文件中的代码。其中一项要求是让代码创建新控件并将其添加到当前窗体。 例如,在Form1.cs中: using System.Windows.Forms; ..
我有此 C++ 代码并将其转换为 C# (.net Framework 4) 代码。有没有人给我一些关于 malloc、free 和 sprintf 方法的提示? int monate = ee; d
我的网络服务器代码有问题 #include #include #include #include #include #include #include int
给定以下 html 代码,将列表中的第三个元素(即“美丽”一词)以斜体显示的 CSS 代码是什么?当然,我可以给这个元素一个 id 或一个 class,但 html 代码必须保持不变。谢谢
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 7 年前。
我试图制作一个宏来避免重复代码和注释。 我试过这个: #define GrowOnPage(any Page, any Component) Component.Width := Page.Surfa
我正在尝试将我的旧 C++ 代码“翻译”成头条新闻所暗示的 C# 代码。问题是我是 C# 中的新手,并不是所有的东西都像 C++ 中那样。在 C++ 中这些解决方案运行良好,但在 C# 中只是不能。我
在 Windows 10 上工作,R 语言的格式化程序似乎没有在 Visual Studio Code 中完成它的工作。我试过R support for Visual Studio Code和 R-T
我正在处理一些报告(计数),我必须获取不同参数的计数。非常简单但乏味。 一个参数的示例查询: qCountsEmployee = ( "select count(*) from %s wher
最近几天我尝试从 d00m 调试网络错误。我开始用尽想法/线索,我希望其他 SO 用户拥有可能有用的宝贵经验。我希望能够提供所有相关信息,但我个人无法控制服务器环境。 整个事情始于用户注意到我们应用程
我有一个 app.js 文件,其中包含如下 dojo amd 模式代码: require(["dojo/dom", ..], function(dom){ dom.byId('someId').i
我对“-gencode”语句中的“code=sm_X”选项有点困惑。 一个例子:NVCC 编译器选项有什么作用 -gencode arch=compute_13,code=sm_13 嵌入库中? 只有
我为我的表格使用 X-editable 框架。 但是我有一些问题。 $(document).ready(function() { $('.access').editable({
我一直在通过本教程学习 flask/python http://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-i-hello-wo
我想将 Vim 和 EMACS 用于 CNC、G 代码和 M 代码。 Vim 或 EMACS 是否有任何语法或模式来处理这种类型的代码? 最佳答案 一些快速搜索使我找到了 this vim 和 thi
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 想改进这个问题?更新问题,使其成为 on-topic对于堆栈溢出。 7年前关闭。 Improve this
这个问题在这里已经有了答案: Enabling markdown highlighting in Vim (5 个回答) 6年前关闭。 当我在 Vim 中编辑包含 Markdown 代码的 READM
我正在 Swift3 iOS 中开发视频应用程序。基本上我必须将视频 Assets 和音频与淡入淡出效果合并为一个并将其保存到 iPhone 画廊。为此,我使用以下方法: private func d
pipeline { agent any stages { stage('Build') { steps { e
我是一名优秀的程序员,十分优秀!