Click here to Skip to main content
15,867,453 members
Articles / Programming Languages / VBScript

VBScript for Reading and Writing to the Windows Host File

Rate me:
Please Sign up or sign in to vote.
4.79/5 (11 votes)
10 Feb 2012CPOL2 min read 127.4K   1.2K   26   8
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.

VBScript
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

VBScript
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Jon A. Casper4-Aug-15 4:56
Jon A. Casper4-Aug-15 4:56 
GeneralMy vote of 5 Pin
Member 868391928-Feb-12 0:43
Member 868391928-Feb-12 0:43 
QuestionSorry Pin
Ludvik Jerabek9-Feb-12 8:53
Ludvik Jerabek9-Feb-12 8:53 
Questionhow to know ip then change hosts file by using vbscript Pin
eng_adwdar7-Feb-12 22:25
eng_adwdar7-Feb-12 22:25 
AnswerRe: how to know ip then change hosts file by using vbscript Pin
Ludvik Jerabek8-Feb-12 15:52
Ludvik Jerabek8-Feb-12 15:52 
Questionwsh error Pin
Member 86198933-Feb-12 13:51
Member 86198933-Feb-12 13:51 
I removed the line because it appears to be part of the comment from the previous line but now nothing happens when I run the .vbs file.
AnswerRe: wsh error Pin
Ludvik Jerabek4-Feb-12 13:03
Ludvik Jerabek4-Feb-12 13:03 
Questionwsh error Pin
Member 86198933-Feb-12 13:44
Member 86198933-Feb-12 13:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.