用于读写 Windows Hosts 文件的 VBScript






4.79/5 (10投票s)
用于读写 Windows hosts 文件的 VBScript。
引言
此脚本允许用户快速添加或删除 Windows 主机文件的条目和主机别名。
背景
这是对一个短脚本的完全重写,该脚本最初编写的目的是为了能够操作 Windows 主机文件。脚本的新版本使用自定义类和字典对象来跟踪 IP 地址和主机别名。整个主机文件被读取到内存数据结构中,以便进行操作。特别注意保留注释信息,这些信息存储在主机条目之上、之下和末尾。虽然注重注释,但目前尚无法直接管理注释数据。
脚本内部原理
VBScript 使用一个自定义类 std_host_file
、字典和数组。std_host_file
类是用于读取和写入主机文件的主要类。创建一个数组来存储 IP 地址、别名和行尾注释。整行注释存储在 std_host_file::m_lines
字典对象中,以及一个定义为 Array(ip , CreateObject("Scripting.Dictionary"), comments) 的数组。VBScript 调用 TypeName
用于确定 std_host_file::m_lines
中的项目是 "String
" 还是 "Variant()
" 对象。此外,std_host_file::m_ip_map
字典对象用于将 IP 地址映射到其各自的 std_host_file::m_lines
键,而 std_host_file::m_alias_map
将别名映射到 IP 地址,用于交叉引用别名。这是必要的,因为现代主机文件可以具有 IPv4 和 IPv6 地址。例如,127.0.0.1 和 ::1 都可以合法地别名为 "localhost"。用户需要执行检查以确保主机别名正确映射到其各自的 IP 地址。未来可以进行增强以实现额外的安全级别。
std_host_file 提供的函数
为了简洁起见,省略了类定义细节和 private
成员函数。
Class std_host_file
' Returns Aliases into aliases if ip has an association. Returns true on success
Public Function GetHostEntryAliases( ByVal ip , ByRef aliases )
' Returns alias IPs into ips if alias has an association. Returns true on success
Public Function GetHostEntryAliasAddresses( ByVal alias , ByRef ips )
' Removes an host entry from the host file. Function returns true on success
Public Function DeleteHostEntry( ByVal ip )
'Removes an alias from an existing host entry. Function returns true on success
Public Function DeleteHostEntryAlias( ByVal ip , ByVal alias )
' Add or appends a host entry to the host file. Function returns true on success
Public Function AddHostEntry( ByVal ip , ByVal alias )
' Returns an array of all IPs to ips. Function returns true if data is returned
Public Function GetAllHostEntryAddresses( ByRef ips )
' Returns an array of all aliases to aliases.
' Function returns true if data is returned
Public Function GetAllHostEntryAliases( ByRef aliases )
' Save a host file. Function returns true if file was opened successfully
Public Function Save( ByVal hostfile )
' Load a host file. Function returns true if file was written successfully
Public Function Load( ByVal hostfile , ByVal bmergecomments )
' Private Member Data
Private m_lines ' Dictionary of host line items
Private m_ip_map ' Dictionary of ip addresses to m_lines key values
Private m_alias_map ' Dictionary of alias mapping to IP addresses
End Class
Using the Code
Option Explicit
Dim o_h : Set o_h = New std_host_file
Call o_h.Load( "C:\Windows\System32\drivers\etc\hosts" , False )
Dim arr , n
Call o_h.AddHostEntry( "192.168.1.5" , "A" )
Call o_h.AddHostEntry( "192.168.1.5" , "B" )
Call o_h.AddHostEntry( "192.168.1.5" , "C" )
Call o_h.AddHostEntry( "192.168.1.5" , "D" )
Call o_h.AddHostEntry( "192.168.1.6" , "E" )
Call o_h.AddHostEntry( "192.168.1.6" , "F" )
Call o_h.AddHostEntry( "192.168.1.6" , "G" )
Call o_h.AddHostEntry( "192.168.1.6" , "H" )
Call o_h.AddHostEntry( "192.168.1.7" , "I" )
Call o_h.AddHostEntry( "192.168.1.7" , "J" )
Call o_h.AddHostEntry( "192.168.1.7" , "K" )
Call o_h.AddHostEntry( "192.168.1.7" , "L" )
Call o_h.DeleteHostEntryAlias( "192.168.1.5" , "A" )
Call o_h.DeleteHostEntryAlias( "192.168.1.5" , "C" )
o_h.DumpData
Call o_h.GetHostEntryAliases( "192.168.1.5" , arr )
' This will show all the aliases for 192.168.1.5
WScript.Echo "Aliases for 192.168.1.5"
For Each n In arr
WScript.Echo n
Next
' This will show if there are multiple addresses for a single alias
Call o_h.GetHostEntryAliasAddresses( "A" , arr )
WScript.Echo "Addresses for test2"
For Each n In arr
WScript.Echo n
Next
' This will show all the aliases in the host file
WScript.Echo "All host aliases"
Call o_h.GetAllHostEntryAliases( arr )
For Each n In arr
WScript.Echo n
Next
' This will show all the IP addresses in the host file
WScript.Echo "All host ip addresses"
Call o_h.GetAllHostEntryAddresses( arr )
For Each n In arr
WScript.Echo n
Next
o_h.DumpData
Call o_h.DeleteHostEntry( "192.168.1.5" )
Call o_h.DeleteHostEntry( "192.168.1.6" )
Call o_h.DeleteHostEntry( "192.168.1.7" )
Call o_h.DeleteHostEntry( "192.168.1.8" )
Call o_h.DeleteHostEntry( "216.10.194.14" )
o_h.DumpData
Call o_h.Save("C:\temp\host.txt")
历史
- 2005\19\08 - 初始发布
- 2012\08\02 - 完全重写
- 2012\09\02 - 移除辅助类
std_host_data
- 2012\10\02 - 修复行尾注释合并错误