Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

VBScript for Reading and Writing to the Windows Host File

0.00/5 (No votes)
10 Feb 2012 1  
VBScript for reading and writing to the Windows host file

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
    ' 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")

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

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here