Introduction
This script allows the user to quickly add or remove host entries and host aliases to a Windows host file.
Background
This is a complete re-write of a short script which was written to be able to manipulate Windows host files. The new version of the script uses a custom class and dictionary objects to keep track of IPs and host aliases. The entire host file is read into an in memory data structure where it can be manipulated. Special care is taken to retain comment information which is stored above, below, and at the end of host entries. Although attention is given to comments, there is currently no ability to manage the comment data directly.
Script Internals
The VBScript utilizes one custom class std_host_file
, dictionaries, and arrays. The std_host_file
class is the primary class used to read and write host files. An array is created to store the IP, aliases, and end of line comments. Whole line comments are stored in the std_host_file::m_lines dictionary
object along with an array defined as Array(ip , CreateObject("Scripting.Dictionary"), comments). The TypeName
VBScript call is used to determine if the item in std_host_file::m_lines
is a "String
" or "Variant()
" object. Additionally std_host_file::m_ip_map dictionary
object is used to map IP addresses to their respective std_host_file::m_lines
key and std_host_file::m_alias_map
maps aliases to IP addresses used to cross reference aliases. This needed to be done since modern host files can have IPV4 and IPV6 addressed. For example, 127.0.0.1 and ::1 can both be legally aliased as "localhost". It remains up to the user to perform checks to ensure that host aliases are properly mapped to their respective IP addresses. Perhaps future enhancements can be done to accomplish an additional level of safety.
Functions Provided by std_host_file
Class definition details and private
member functions have been omitted for brevity.
Class std_host_file
Public Function GetHostEntryAliases( ByVal ip , ByRef aliases )
Public Function GetHostEntryAliasAddresses( ByVal alias , ByRef ips )
Public Function DeleteHostEntry( ByVal ip )
Public Function DeleteHostEntryAlias( ByVal ip , ByVal alias )
Public Function AddHostEntry( ByVal ip , ByVal alias )
Public Function GetAllHostEntryAddresses( ByRef ips )
Public Function GetAllHostEntryAliases( ByRef aliases )
Public Function Save( ByVal hostfile )
Public Function Load( ByVal hostfile , ByVal bmergecomments )
Private m_lines Private m_ip_map Private m_alias_map 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 )
WScript.Echo "Aliases for 192.168.1.5"
For Each n In arr
WScript.Echo n
Next
Call o_h.GetHostEntryAliasAddresses( "A" , arr )
WScript.Echo "Addresses for test2"
For Each n In arr
WScript.Echo n
Next
WScript.Echo "All host aliases"
Call o_h.GetAllHostEntryAliases( arr )
For Each n In arr
WScript.Echo n
Next
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")
History
- 08\19\2005 - Initial release
- 02\08\2012 - Complete rewrite
- 02\09\2012 - Removed helper class
std_host_data
- 02\10\2012 - Fixed bug with end of line comment merge