Kaseya Community

MVPS.org Host File Download & Update Script

  • Hey guys,

    I thought it would be a great idea to create a script to download the most up to date HOST file from http://www.mvps.org/winhelp2002/hosts.htm

    They have already done the hard work, they have the HOST File (update regularly) and they have the bat file to install it.

    I have attempted to create a script to WGET the host file, and run the bat file to install it. Its not working. I think its because of path issues, but I cant figure out the best way to fix it.

    Hopefully someone can take what I have put together and make it a great script that we can all share.

    Script Name: MVPS.org Host File Download & Update
    Script Description:

    IF True
    THEN
    Get Variable
    Parameter 1 : 10
    Parameter 2 :
    Parameter 3 : AgentTemp
    OS Type : 0
    Write File
    Parameter 1 : #AgentTemp#\\wget.exe
    Parameter 2 : VSASharedFiles\wget.exe
    OS Type : 0
    Execute File
    Parameter 1 : #AgentTemp#\wget.exe
    Parameter 2 : -t 45 -O #AgentTemp#\HOSTS http://www.mvps.org/winhelp2002/hosts.txt
    Parameter 3 : 3
    OS Type : 0
    Write File
    Parameter 1 : #AgentTemp#\writehost.bat
    Parameter 2 : VSASharedFiles\writehost.bat
    OS Type : 0
    Execute File
    Parameter 1 : #AgentTemp#\writehost.bat
    Parameter 2 :
    Parameter 3 : 3
    OS Type : 0
    ELSE


    Legacy Forum Name: MVPS.org Host File Download & Update Script,
    Legacy Posted By Username: Resistance2Fly
  • I just threw this together. I might just roll it out, too.

    Ideally, you should keep curl-nossl up to date, instead of rolling it out in any script that requires a file download because its ~300KB/client. (if you try to run this script on 500 clients at once, that's a ~150MB transfer.)

    If you do have it up to date, just remove write file.


    Script Name: Update Hosts File MVPS.org
    Script Description: Downloads latest hosts.txt file from MVPS.org and writes it to C:\windows\system32\etc\drivers\hosts

    IF True
    THEN
    Get Variable
    Parameter 1 : 2
    Parameter 2 : http://www.mvps.org/winhelp2002/hosts.txt
    Parameter 3 : URL
    OS Type : 0
    Get Variable
    Parameter 1 : 10
    Parameter 2 :
    Parameter 3 : TempDir
    OS Type : 0
    Write File
    Parameter 1 : #TempDir#\curl-nossl.exe
    Parameter 2 : VSASharedFiles\curl-nossl.exe
    OS Type : 0
    Execute File
    Parameter 1 : #TempDir#\curl-nossl.exe
    Parameter 2 : #URL# -o "%SYSTEMROOT%\system32\drivers\etc\hosts"
    Parameter 3 : 3
    OS Type : 0
    Write Script Log Entry
    Parameter 1 : Downloaded latest hosts file update from MVPS.org
    OS Type : 0
    ELSE


    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: nevesis
  • i like this...good work

    i utilise the hosts file a lot so i'll post my script for merging with existing hosts file when i get time

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: tbone2345
  • Tbone, merging the files sounds like a great idea, let us know how that works out for you!

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Resistance2Fly
  • UPDATE:

    the file from MVPS.org includes blocks for googleadservices, which makes the paid ads in google fail, so you may want to remove the following:

    127.0.0.1 pagead.googlesyndication.com
    127.0.0.1 pagead2.googlesyndication.com #[Google AdWords]
    127.0.0.1 adservices.google.com
    127.0.0.1 video-stats.video.google.com
    # 127.0.0.1 ssl.google-analytics.com #[urchinTracker][disabled - Firefox issues]
    127.0.0.1 www.google-analytics.com #[Google Analytics]
    127.0.0.1 4.afs.googleadservices.com
    127.0.0.1 imageads.googleadservices.com #[Ewido.TrackingCookie.Googleadservices]
    127.0.0.1 partner.googleadservices.com
    127.0.0.1 www.googleadservices.com


    also, has anyone had any luck with merging the files?

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Resistance2Fly
  • Here is my attempt at this. This one will merge the MVPS host file with your existing host file and then remove any duplicate entries. Its a combination of script tid-bits I found on these forums, technet, and various other sites.

    My goal here was to be able to schedule this script to run and grab the latest hosts file from MVPS.org each time. However, I ran into a problem that their data was always appended to the end of the file. So, I wrote a VB script to go in and clean it all up after the merge.

    Any contributions to help clean up and/or bullet-proof this script are welcome.

    Steve

    Script Name: Block Malicious Sites via host file
    Script Description: This script will go to http://www.mvps.org/winhelp2002/hosts.txt and then update the hosts file on the local machine. Then a vbscript will go thought and clean out any of the duplicate entries. This allow for this script to be scheduled to run on a re-occuring schedule and not have the hosts file get enormous. If running on Vista, UAC will need to be turned off for this to work automatically.

    IF Test File
    Parameter 1 : %windir%\system32\drivers\etc\hosts
    Exists :
    THEN
    Execute Shell Command
    Parameter 1 : copy %windir%\system32\drivers\etc\hosts %windir%\system32\drivers\etc\hosts.bak
    Parameter 2 : 1
    OS Type : 13
    Get File
    Parameter 1 : %windir%\system32\drivers\etc\hosts.bak
    Parameter 2 : ..\Docs\hosts.bak
    Parameter 3 : 3
    OS Type : 13
    Execute Shell Command
    Parameter 1 : attrib -r %windir%\system32\drivers\etc\hosts
    Parameter 2 : 1
    OS Type : 13
    Get URL
    Parameter 1 : http://www.mvps.org/winhelp2002/hosts.txt
    Parameter 2 : #vAgentConfiguration.AgentTempDir#\MVPShosts.txt
    Parameter 3 : 3
    OS Type : 13
    Execute Shell Command
    Parameter 1 : type "#vAgentConfiguration.AgentTempDir#\MVPShosts.txt" >>>> %windir%\system32\drivers\etc\hosts
    Parameter 2 : 1
    OS Type : 13
    Write File
    Parameter 1 : #vAgentConfiguration.AgentTempDir#\RemoveDuplicateLines.vbs
    Parameter 2 : VSASharedFiles\RemoveDuplicateLines.vbs
    OS Type : 13
    Execute Shell Command
    Parameter 1 : wscript.exe "#vAgentConfiguration.AgentTempDir#\RemoveDuplicateLines.vbs"
    Parameter 2 : 1
    OS Type : 13
    Execute Shell Command
    Parameter 1 : attrib +r %windir%\system32\drivers\etc\hosts
    Parameter 2 : 1
    OS Type : 13
    Write Script Log Entry
    Parameter 1 : Block Malicious Sites Completed
    OS Type : 13
    ELSE
    Write Script Log Entry
    Parameter 1 : Block Malicious Sites failed because the hosts file does not exist.
    OS Type : 0



    The Kaseya script calls a VB Script I made. Here is the code for that script. You'll need to upload this to your Kaseya server.


    Const ForReading = 1
    Const ForWriting = 2

    Set objDictionary = CreateObject("Scripting.Dictionary")

    'get the %windir% environment variable
    Set oshell = CreateObject( "WScript.Shell" )
    windir=oShell.ExpandEnvironmentStrings("%Windir%")
    HostsDir = windir & "\system32\drivers\etc\"


    Set objFSO = CreateObject("Scripting.FileSystemObject")

    'delete our old hosts.txt file if we ran this script before
    If objFSO.FileExists (windir& "\system32\drivers\etc\hosts.txt") then
    oShell.Run("attrib -r " & HostsDir & "hosts.txt")
    objFSO.DeleteFile windir& "\system32\drivers\etc\hosts.txt"
    End If

    'rename the hosts file to host.txt b/c for some reason I coulnt manipulate the hosts file directly.
    objFSO.MoveFile windir& "\system32\drivers\etc\hosts" , windir & "\system32\drivers\etc\hosts.txt"

    Set objFile = objFSO.OpenTextFile _
    (windir & "\system32\drivers\etc\hosts.txt", ForReading)

    'make our output file
    objFSO.CreateTextFile windir & "\system32\drivers\etc\hosts_clean.txt"

    Do Until objFile.AtEndOfStream
    strName = objFile.ReadLine
    If Not objDictionary.Exists(strName) Then
    objDictionary.Add strName, strName
    End If
    Loop

    objFile.Close

    Set objFile = objFSO.OpenTextFile _
    (windir & "\system32\drivers\etc\hosts_clean.txt", ForWriting)

    For Each strKey in objDictionary.Keys
    objFile.WriteLine strKey
    Next

    objFile.Close

    'rename our cleaned file to our hosts file
    objFSO.MoveFile windir & "\system32\drivers\etc\hosts_clean.txt" , windir & "\system32\drivers\etc\hosts"


    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: ARNNTW
  • keep in mind, dudes, that if your host file is too large it can slow the system down big time so it helps to stop the DNS client service to avoid this. DNS client stopping wont do anything bad your system still does all dns lookups.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: saybrook
  • I find that the MVPS.org host file blocks many legitimate sites... or parts of sites from working. Beware.

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: Resistance2Fly
  • Been running the mvps.org hosts script for about a month now and no complaints from end users. They have just mentioned that some sites don't display ads which was intended.

    Steve

    Legacy Forum Name: Scripts Forum,
    Legacy Posted By Username: ARNNTW