Announcement

Collapse
No announcement yet.

Need to write to "hosts" files on many pc's

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Need to write to "hosts" files on many pc's

    OK,
    I have many pc's I need to update the hosts file.
    I usually just copy one from a server, but it requires a reboot to take effect, (Documented in MSKB, can't find the link right now, thx ClintD from Arstechnica)
    so instead of copying, does anyone know how to add a line to the file on multiple machines through batch file or the like or whatever? So I don't have to reboot?

  • #2
    Re: Need to write to "hosts" files on many pc's

    I don't know about the rebooting part, but there are a few ways to append files from a batch file. One way I can't remember, but the command "help copy" will give you some clues. You can also "type filename >> filename" to append data.

    If you can work out whether you need to reboot or not, the appending part should be easy. Perhaps someone else has some other ideas.
    Kevin Stanush
    SystemTools Software Inc.

    Comment


    • #3
      Re: Need to write to "hosts" files on many pc's

      YES, I do have to reboot the machines when I overwrite the hosts file(copy from server).
      I need to append it, but do not know how....
      ANyone?

      Comment


      • #4
        Re: Need to write to "hosts" files on many pc's

        Using PSEXEC, I'm sure you could write a batch that appends the data you need, line by line.

        Using what Kevin says above, " >> ", you can append data to a file. Simply ECHO the information.

        For example:

        hosts.txt contains:

        127.0.0.1 localhost

        if you run the following as a tool in Hyena:

        "cmd /c cls & psexec \\%E% echo 123.1.2.3 myserver >> c:\winnt\system32\drivers\etc\hosts."

        it should append the line "123.1.2.3 myserver" to the hosts file.

        I did NOT test this so it obviously may need some tweaking.

        Comment


        • #5
          Re: Need to write to "hosts" files on many pc's

          Ok, here it is.


          If they really need a DAT style. I'll have to look into how to detect the OS remotely. I'm 99% positive this can be done as my DetectOS only uses the registry to do it. I'm tired and in the middle of installing XP so I'll have to do it some other time though.

          This came from my programer at work. It is a local version so you can use it in your login script or you can execute it with psexec. Your choice.

          ' YOUR FREE TO USE AND MODIFY THIS SCRIPT WITHOUT
          ' RESTRICTION. NO WARRANTY IMPLIED OR OFFERED.
          ' SOME OF THIS CODE IS DERIVED FROM OTHER WORKS
          ' AND I CLAIM NO CREDIT TO THEIR CREATIONS.

          ' ------------------------------------------------------
          ' SCRIPT NAME: Add a DNS entry to the HOSTS file script.
          ' DATE : 04/04/2003
          ' AUTHOR : Chris Henson
          ' ------------------------------------------------------
          '
          ' This is a combination of a couple other
          ' scripts that I've made for our IT staff.
          ' I've never actually run *this* script, but
          ' it should work out of the box.
          '
          ' This script is setup to modify the HOSTS file
          ' on the computer (locally) that it is run on. It
          ' is suitable for a login script or a remote execute
          ' (like with the psexec tool).
          '
          ' Scroll down to the ' >>> CHANGE THIS <<< line
          ' and change the value of the NewDNSEntry to what
          ' you want to add to the HOSTS file.
          '
          ' NOTE: If it already exists in the HOST file it
          ' will add a duplicate. It doesn't check.
          '
          ' NOTE: There is no error checking on this script.
          ' Blame a lazy programmer


          Const ForReading = 1

          '################################################# ########################
          ' This will detect the OS that the script is running on via checking the
          ' registry.
          '
          ' Returns WIN95,WIN98,WINNT4,WIN2000,WINXP,Unknown
          Public Function DetectOS()
          Dim WSHShell
          Dim NTName
          Dim NTVersion
          Dim NTSP
          Dim WINName
          Dim WINVersion

          Set WSHShell = WScript.CreateObject("WScript.Shell")

          On Error Resume Next
          ' Get NT Registry Info
          NTName = WSHShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\ProductName")
          NTVersion = WSHShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentVersion") & "." & WSHShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CurrentBuildNumber")
          NTSP = WSHShell.RegRead("HKLM\Software\Microsoft\Windows NT\CurrentVersion\CSDVersion")

          ' Get Win9X Registry Info
          WINName = WSHShell.RegRead("HKLM\Software\Microsoft\Windows\ CurrentVersion\Version")
          WINVersion = WSHShell.RegRead("HKLM\Software\Microsoft\Windows\ CurrentVersion\VersionNumber") & " " & WSHShell.RegRead("HKLM\Software\Microsoft\Windows\ CurrentVersion\SubVersionNumber")
          On Error Goto 0

          ' Determine Operating System
          If Trim(WINName) <> "" Then
          If instr(1, WINName, "95") > 0 Then
          DetectOS = "WIN95"
          Else
          DetectOS = "WIN98"
          End if
          Else
          If NTName = "" Then
          DetectOS = "WINNT4"
          Else
          If Instr(1,NTName, "2000") > 0 Then
          DetectOS = "WIN2000"
          ElseIf Instr(1,NTName, "XP") > 0 Then
          DetectOS = "WINXP"
          Else
          DetectOS = "Unknown"
          End If
          End If
          End If

          End Function

          '################################################# ########################
          ' Trims all whitespace characters from the front and end of a string.
          ' Spaces, Tabs, Linefeeds, and carrige returns are trimmed.
          Public Function TrimWhiteSpace(txt)
          Dim FirstGoodChar
          Dim LastGoodChar
          Dim i
          Dim ch

          FirstGoodChar = 1
          LastGoodChar = Len(txt)

          For i = 1 To Len(txt)
          ch = Asc(Mid(txt, i, 1))
          If Not (ch = 32 Or ch = 9 Or ch = 10 Or ch = 13) Then
          FirstGoodChar = i
          Exit for
          End If
          Next

          For i = Len(txt) To 1 Step -1
          ch = Asc(Mid(txt, i, 1))
          If Not (ch = 32 Or ch = 9 Or ch = 10 Or ch = 13) Then
          LastGoodChar = i
          Exit for
          End If
          Next

          TrimWhiteSpace = Mid(txt, FirstGoodChar, LastGoodChar - FirstGoodChar + 1)
          End Function

          '################################################# ########################
          ' MAIN SCRIPT BODY
          '################################################# ########################
          Dim fso, ts, f1
          Dim Buffer, Output
          Dim OSType
          Dim HostPath
          Dim NewDNSEntry

          ' >>> CHANGE THIS <<<
          NewDNSEntry = "MyShinyNewServer 125.125.125.125"

          ' Create our FSO so we can interact with the filesystem.
          Set fso = CreateObject("Scripting.FileSystemObject")

          ' Figure out what OS this script is running on so we
          ' can locate the HOSTS file.
          OSType = DetectOS()
          If OSType = "WINNT4" Or OSType = "WIN2000" Then
          HostPath = "C:\WINNT\SYSTEM32\DRIVERS\ETC\HOSTS"
          ElseIf OSType = "WINXP" Then
          HostPath = "C:\WINDOWS\SYSTEM32\DRIVERS\ETC\HOSTS"
          Else
          HostPath = "C:\WINDOWS\HOSTS"
          End If

          ' Does the HOSTS file exist?
          If fso.FileExists(HostPath) Then
          ' Open the HOSTS file and setup a stream to read from it.
          Set f1 = fso.GetFile(HostPath)
          Set ts = f1.OpenAsTextStream(ForReading)

          Do While Not ts.AtEndOfStream
          buffer = ts.ReadLine ' Get a line from the HOSTS file

          If Mid(TrimWhiteSpace(Buffer),1,1) = "#" Then ' Check to see if line is a comment
          Output = Output & Buffer & vbCRLF
          ElseIf Len(TrimWhiteSpace(Buffer)) = 0 Then ' Check for a blank line (all whitespace or just crlf)
          Output = Output & vbCRLF

          ' You could also have this script look for a particalur DNS entry
          ' and comment it out. For example the following would find any DNS
          ' entry for ARTHUR and then put a # in front of it. Thus commenting
          ' it out.
          'ElseIf Instr(1,TrimWhiteSpace(UCase(Buffer)),"ARTHUR") > 0 Then
          ' Output = Output & "# " & Buffer & vbcrlf

          Else ' It was a normal line
          Output = Output & Buffer & vbcrlf
          End If

          Loop

          ts.Close
          Set f1 = Nothing

          ' Now we have a copy of the HOSTS file in memory (stored in a string
          ' called Output). At this point we are going to add a new entry to the
          ' end of it and then re-write the HOSTS file (overwriting the existing
          ' one).

          Output = Output & NewDNSEntry & vbCRLF
          Set f1 = fso.CreateTextFile(HostPath, True)
          f1.Write Output
          f1.Close

          WScript.Echo "HOSTS file has been updated."
          Else
          ' No HOSTS file was found. We'll assume were looking in the right
          ' place and it just doesn't exist and will create one and add our
          ' entry to it.

          Set f1 = fso.CreateTextFile(HostPath, True)
          f1.WriteLine NewDNSEntry & vbCRLF
          f1.Close

          WScript.Echo "HOSTS file was created."
          End If

          Comment

          Working...
          X