Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Edit "hosts" file on PC so that I can add text to the file

Status
Not open for further replies.

Dublin73

IS-IT--Management
Apr 26, 2005
236
US
Hi, I'm scouring the internet for a solution to this one with no luck. I want to apply a startup VBScript sript to our Windows 2K domain computers via GPO that does the following....

Locates the file C:\WINNT\system32\drivers\etc\hosts

and then edits this hosts file by leaving the current contents of the file intact, but adds the following text

192.168.0.2 server1.domain.com
192.168.0.3 server2.domain.com

then saves the file.

For example,

The "hosts" file before...

# Copyright (c) 1993-1999 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

127.0.0.1 localhost

-----------------------------------------------------------

and now the same "hosts" file after the script has been run....

# Copyright (c) 1993-1999 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

127.0.0.1 localhost
192.168.0.2 server1.domain.com
192.168.0.3 server2.domain.com

------------------------------------------------------------

I can do this manually at the PCs using Notepad to edit the hosts file, but obviously a script will make life so much easier!

thanks in advance to anyone who may take the time to help.
 
Easy enough to do, but why is it even necessary? Create Host records in your internal DNS and you will only have one place to worry about.

If you still want a script I can help but DNS is the best solution for this.

I hope you find this post helpful.

Regards,

Mark
 
Hi Mark, thanks for responding! I wish DNS could do this, but unfortunately it's a bit more complicated. See link...


FYI there's an error in my post. Subnet B has a network ID of 192.168.0.0/24 as opposed to what I have posted, 153.213.4.0/24

I really didn't want to go down the road of editing hosts files, but it's a last resort.
 
whats the deal with the default gateways on the clients/servers? does this allow the functionality? or are you allowing the servers to register their 192 addresses in your DNS and this is causing you issues?

anyway, i am no networking geek so i should prob put a sock in it ;-) i am having to do something similar here, but i wont bore you with it.

still, i cant believe there isnt a networking solution which doesnt rely on lmhosts but there you go.

All you need to do is to
a)
1. FSO.OpenTextFile(LMHOST) and read the current state into memory, (Array, dictionary)
2. move the LMHOSTS sideways a backup
3. FSO.CreateTestFile with the stuff in memory and slip in your changes...

b)
1.Open you LMHOSTS file with 'Appending' as the option,
and tag your new lines onto the end (this will only work if you dont mind your amendments going onto the end of the file)
 
mrmovie, thanks for responding.

The networks complex. There are two subnets in the main office. The remote office is a third subnet. The remote office can only access servers in the main office through NATed addresses.

Since DNS is AD Integrated, all DNS records on the main office domain controller are transferred over to the remote office domain controller when AD is transferred.

I'll see what I can do with your suggestion. I'm a novice at best w/ VBScript so don't really know how to write what you've listed above. I'll give it a go though.

thanks again.
 
Code:
[green]
'==========================================================================
'
' NAME: AppendToHostsFile.vbs 
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL: [URL unfurl="true"]http://www.TheSpidersParlor.com[/URL]
' COPYWRITE (c) 2005 All Rights Reserved
' DATE  : 2/9/2006
'
' COMMENT: 
'
'==========================================================================
[/green]Const ForReading = 1, ForWriting = 2, ForAppending = 8 
Set fso = CreateObject("Scripting.FileSystemObject")
Set WshShell=CreateObject("WScript.Shell")
WinDir =WshShell.ExpandEnvironmentStrings("%WinDir%")

HostsFile = WinDir & "\System32\Drivers\etc\Hosts"
[green]
'Check the date of the file to see if we have already modified it.[/green]
Set DateCheck = fso.GetFile(HostsFile)
[green]'Set the date and time the file needs to be older than.[/green]
If DateCheck.DateLastModified < "2/9/2006 6:50:00 AM" Then
[green]	'File is old and needs to be modified.[/green]
	Set filetxt = fso.OpenTextFile(HostsFile, ForAppending, True)
	filetxt.WriteLine("192.168.0.2   server1.domain.com  ")
	filetxt.WriteLine("192.168.0.3   server2.domain.com  ")
	filetxt.Close 
End If
WScript.quit

I hope you find this post helpful.

Regards,

Mark
 
i guess from a dns view would it require you to create different zones for your different locations?
 
Mark, you're a legend! Thanks a bunch for that. Works like a charm. I'm going to roll it out to the client PCs via Windows GPO as a startup script.

In the past I've created scripts to copy files onto the client PCs and replace shortcuts if they existed, things like that, but this one was a bit advanced for a novice like myself.

thanks again!

mrmovie thanks for responding also. We've only got one zone for our domain and it's AD Integrated, so all of our Domain Controllers will have the same DNS info.. To the best of my knowledge, we wouldn't be able to resolve the issue with a second zone? The zone name has the same name as our DNS domain. This name in turn is the DNS suffix that all computers in the domain have appended to them etc..

I don't think that you can specify a forward lookup zone by subnet as opposed to domain name etc.. It would be nice if you could though for cases like this.

cheers guys!
 
Thanks Dublin.

Only a legend in my own mind I think. Happy to be able to help.

Mark

I hope you find this post helpful.

Regards,

Mark
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top