Using VBS to realize the one click configuration code of hosts file

Time:2021-10-21

First, let’s talk about how to enter the hosts file. The location of the hosts file in the computer under the windows environment (I use a 32-bit win7) is in the directory% windir% \ system32 \ drivers \ etc \, the file name is hosts, and there is no extension. However, instead of requiring many directories each time to find the hosts file, we can directly open the hosts file with Notepad by executing the following bat script:

?
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

Call the bat script host.bat and put it in C: \ windows \ system32. You can directly enter the host command on the command line or in the start menu of win7 to open the hosts file.

To get back to business, let me talk about how to automatically insert records after the hosts file.

The following bat script can meet the simplest hosts configuration, that is, add a record at the end of the hosts file:

?
1
2
3
4
5
6
@attrib -r "%windir%\System32\drivers\etc\hosts"
@echo ######Host configuration 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 configuration end > > "% windir% \ system32 \ drivers \ etc \ hosts"
::@attrib +r "%windir%\System32\drivers\etc\hosts"

The configuration effect is as follows:

Using VBS to realize the one click configuration code of hosts file

This method is very simple, but there is also a disadvantage of using this method, that is, mapping records may be configured repeatedly.

So I tried to write the following one that can automatically configure the specified URL hostsVBSScript hosthelper.vbs, the code is as follows:

?
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 file configuration tool
'author: tsybius 2014
'time: October 20, 2015
'description: hosthelper is a host file configuration tool. The input is the host file address, IP address and domain name
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
 
'force explicit declaration of all variables in the module
Option Explicit
 
'read parameters
Dim strHostAddr 'host file address
Dim strIpAddr  'IP address
Dim strName   'host name
Dim strOper   'operation type cover: write append: append
If WScript.Arguments.Count <> 4 Then
  WScript.Echo "Parameter error"
  WScript.Quit
Else
  'read in parameters
  strHostAddr = WScript.Arguments(0) 'parameter 1: host file address
  strIpAddr = WScript.Arguments(1)  'parameter 2: IP address
  strName = WScript.Arguments(2)   'parameter 3: hostname
  strOper = WScript.Arguments(3)   'parameter 4: write policy cover: overwrite append: append
  'override: exclusive join
  'append: add the corresponding relationship between IP address and host name at the end of the file
  'determine write policy
  Dim strOperName
  If strOper = "cover" Then
    strOperName = Overwrite
  ElseIf strOper = "append" Then
    strOperName = "Append"
  Else
    WScript.Echo "Illegal write policy!"
    WScript.Quit
  End If
  'display input information
  WScript.Echo Host file address: & strHostAddr
  WScript.Echo IP address: & strIpAddr
  WScript.Echo Host name: & strName
  WScript.Echo Write policy: & strOperName
  WScript.Echo ""
End If
 
Dim FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1, ForWriting = 2, ForAppending = 8
 
'traverse the host file to determine whether the specified host value already exists
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
      'if the IP is the same and the domain name is the same, it is considered that the host to be configured already exists
      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 "The host you want to configure already exists!"
  WScript.Quit
End If
 
'write the correspondence between the IP address and the domain name to the host
If strOper = "cover" Then
 
  'write policy: overwrite
  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 "Overwrite complete"
  
ElseIf strOper = "append" Then
 
  'write policy: append
  Dim fileAppend
  Set fileAppend = FSO.OpenTextFile(strHostAddr, ForAppending, False)
  fileAppend.WriteLine
  fileAppend.WriteLine strIpAddr & " " & strName
  WScript.Echo "Append complete"
  fileAppend.Close
 
End If

The function of this vbs script is to pass in the hosts file address, IP address and host name, and specify the write strategy (including overwrite and append). After executing the script, the hosts file will be automatically configured.

In order to run this vbs script better, I wrote a bat batch command line to execute it. The code is as follows:

?
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

This script attempts to append the last record to the hosts file. The domain name is www.tsybius2014.com, the IP address is 127.0.0.1, the write policy is append, and the hosts file is backed up before writing.

The execution effect of this script is as follows:

Using VBS to realize the one click configuration code of hosts file

After entering the hosts file, you can see that the mapping has been written at the end of hosts.txt

Using VBS to realize the one click configuration code of hosts file

Note: because the code in this article has only been tested simply, some codes may not be robust enough, so they should be used with caution in actual use.