Click here to Skip to main content
Click here to Skip to main content

VBScript for Reading and Writing to the Windows Host File

, 10 Feb 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
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, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Ludvik Jerabek
Software Developer
United States United States
No Biography provided

Comments and Discussions

 
GeneralMy vote of 5 PinmemberMember 868391928-Feb-12 1:43 
QuestionSorry PinmemberLudvik Jerabek9-Feb-12 9:53 
Questionhow to know ip then change hosts file by using vbscript Pinmembereng_adwdar7-Feb-12 23:25 
Hi Everybody
i have oracle servers in my environment
user can using oracle application in his [ office or Home ]
i need your help to edit in hosts file as the following
case (1) in office
-----------------------------------
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
 
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
10.201.10.81 nsfaoebsp01.ns.local production
10.201.10.83 nsfaoebst01.ns.local testserver
10.101.10.120 nshooebst01.ns.local test2
#217.139.71.217 nsfaoebsp01.ns.local production
#217.139.71.220 nsfaoebst01.ns.local testserver
=========================================================================================
case (2)in home
 
# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
# 102.54.94.97 rhino.acme.com # source server
# 38.25.63.10 x.acme.com # x client host
 
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
#10.201.10.81 nsfaoebsp01.ns.local production
#10.201.10.83 nsfaoebst01.ns.local testserver
#10.101.10.120 nshooebst01.ns.local test2
217.139.71.217 nsfaoebsp01.ns.local production
217.139.71.220 nsfaoebst01.ns.local testserver
 
=======================================================================================================
but these vbs file will take Decision by check the IP Machine
if the machine taken ip in rang 10.201.X.X or 10.101.X.X
vbs will using case 1
if machine take any IP expect 10.201.x.X or 10.101.X.X
Using case 2
AnswerRe: how to know ip then change hosts file by using vbscript PinmemberLudvik Jerabek8-Feb-12 16:52 
Questionwsh error PinmemberMember 86198933-Feb-12 14:51 
AnswerRe: wsh error PinmemberLudvik Jerabek4-Feb-12 14:03 
Questionwsh error PinmemberMember 86198933-Feb-12 14:44 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411019.1 | Last Updated 10 Feb 2012
Article Copyright 2005 by Ludvik Jerabek
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid