Kae Travis

Add and Remove Host Entries

Tags:

This is a quick script I’ve used to add and remove hosts entries to the windows hosts file. It ignores commented lines (lines starting with a hash (#)), and it also doesn’t validate the format of the host or IP address so use with care!

Add And Remove Host Entries

cscript.exe hosts.vbs “Add” “10.10.10.10” “example”
cscript.exe hosts.vbs “Remove” “10.10.10.10”

Option Explicit
Const ForReading = 1
Const ForWriting = 2
Const ReadOnly = 1
'Usage
'cscript.exe hosts.vbs "Add" "10.10.10.10" "example" 
'cscript.exe hosts.vbs "Remove" "10.10.10.10"
If (wscript.arguments.count < 2) Then
'need a verb, host as a minimum
Wscript.quit
End If
'Add/Remove
dim inputAction : inputAction = wscript.arguments(0)
dim inputIPAddress : inputIPAddress = wscript.arguments(1)
'host can be blank if removing
dim inputHost
if (wscript.arguments.count > 2) Then
inputHost = wscript.arguments(2)
End If
dim oFSO : Set oFSO = CreateObject("Scripting.FileSystemObject")
dim objShell : Set objShell = CreateObject("WScript.Shell")
dim strSystemRoot : strSystemRoot = objShell.ExpandEnvironmentStrings("%systemroot%")
dim pathToHosts : pathToHosts = strSystemRoot & "\system32\drivers\etc\hosts"
'make changes in memory first - we need to open it in write mode first otherwise we get 'Bad file mode' error
dim hostsFile : Set hostsFile = oFSO.OpenTextFile(pathToHosts, ForReading, true)
'get original read from file.  We'll amend hosts in memory until we commit changes at the end
dim hostsContent
If Not hostsFile.AtEndOfStream Then hostsContent = hostsFile.ReadAll
dim hostEntry
if (inputAction = "Add") Then
hostsContent = AddHostEntry(inputIPAddress,inputHost)
End If
if (inputAction = "Remove") Then
hostsContent = RemoveHostEntry(inputIPAddress)
End If
hostsFile.Close()
'set attribute to read/write if readonly
dim objFile : Set objFile = oFSO.GetFile(pathToHosts)
If objFile.Attributes AND ReadOnly Then
objFile.Attributes = objFile.Attributes XOR ReadOnly
End If
'then write changes to actual file
Set hostsFile = oFSO.OpenTextFile(pathToHosts, ForWriting, true)
hostsFile.Write hostsContent
hostsFile.Close()
Function RemoveHostEntry(ipAddress)
dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "^(?!#)(" & ipAddress & ")(\s|\t)+"
dim strNewContents : strNewContents = ""
dim hostsContentLines : hostsContentLines = Split(hostsContent,vbCrlf)
For each hostEntry in hostsContentLines
If NOT objRegEx.Test(hostEntry) Then	
strNewContents = strNewContents & hostEntry & vbCrLf		
End If
Next
'remove last carriage return	
If Right(strNewContents, 2) = vbCrLf Then
strNewContents = Left(strNewContents, Len(strNewContents) - 2)
End If
Set objRegEx = Nothing
RemoveHostEntry = strNewContents
End Function
Function AddHostEntry(ipAddress, hostName)
dim objRegEx : Set objRegEx = CreateObject("VBScript.RegExp")
objRegEx.Pattern = "^(?!#)(" & ipAddress & ")(\s|\t)+"
dim strNewContents : strNewContents = ""
dim hostnameExists : hostnameExists = false	
dim hostsContentLines : hostsContentLines = Split(hostsContent,vbCrlf)
For each hostEntry in hostsContentLines
If objRegEx.Test(hostEntry) Then
hostnameExists = true	
End If
strNewContents = strNewContents & hostEntry & vbCrLf
Next
If Not hostnameExists Then
strNewContents = strNewContents & ipAddress & vbTab & hostName 
End If
'remove last carriage return	
If Right(strNewContents, 2) = vbCrLf Then
strNewContents = Left(strNewContents, Len(strNewContents) - 2)
End If
Set objRegEx = Nothing
AddHostEntry = strNewContents
End Function
Set hostsFile = Nothing
Set objFile = Nothing
Set oFSO = Nothing
Set objShell = Nothing

 

Add and Remove Host Entries
Add and Remove Host Entries

Leave a Reply