Jump to content

Hosts File Editor


Recommended Posts

Hi all,

trying to get this to work, it doesnt

i am trying to edit my hosts file

the file handle opening it was valid,

then i tried to navigate through the file , file readline doesnt get out of the loop at all..

Dim $line, $HostFileName ,$FileContent 

$FileContent = ""

    IF @OSVersion = "WIN_XP" Then
        $HostFileName = "C:\Windows\System32\Drivers\etc\Hosts"
        
        $FileHandle = Fileopen($HostFileName,1)
    
        IF Not $FileHandle Then
            Msgbox(4096,"Error", "Please contact your administrator - Hosts file could not be accessed")
            Exit
        EndIf
        
    ; Read in lines of text until the EOF is reached check if it has been configured already.
        While 1
            $line = FileReadLine($FileHandle)
                If @error = -1 Then ExitLoop
                $FileContent = @CRLF & $line 
        Wend            
    MSgbox (4096,"",$FileContent) 
    Else
        
        Msgbox(4096, "Fatal Error","Unsupported OS, Script will run only on Windows XP")
        Exit
        
    EndIf

where am i wrong??

Edited by rajeshontheweb
Link to comment
Share on other sites

Dim $line, $HostFileName, $FileContent

$FileContent = ""

If @OSVersion = "WIN_XP" Then
    $HostFileName = @SystemDir & "\Drivers\etc\hosts"

    $FileHandle = FileOpen($HostFileName, 0) ; 0 = open in read mode

    If Not $FileHandle Then
        MsgBox(4096, "Error", "Please contact your administrator - Hosts file could not be accessed")
        Exit
    EndIf

    ; Read in lines of text until the EOF is reached check if it has been configured already.
    While 1
        $line = FileReadLine($FileHandle)
        If @error = -1 Then ExitLoop
        $FileContent &=  $line & @CRLF ; Fixed the way you were appending too
    WEnd
    MsgBox(4096, "", $FileContent)
Else

    MsgBox(4096, "Fatal Error", "Unsupported OS, Script will run only on Windows XP")
    Exit

EndIf

[u]Helpful tips:[/u]If you want better answers to your questions, take the time to reproduce your issue in a small "stand alone" example script whenever possible. Also, make sure you tell us 1) what you tried, 2) what you expected to happen, and 3) what happened instead.[u]Useful links:[/u]BrettF's update to LxP's "How to AutoIt" pdfValuater's Autoit 1-2-3 Download page for the latest versions of Autoit and SciTE[quote]<glyph> For example - if you came in here asking "how do I use a jackhammer" we might ask "why do you need to use a jackhammer"<glyph> If the answer to the latter question is "to knock my grandmother's head off to let out the evil spirits that gave her cancer", then maybe the problem is actually unrelated to jackhammers[/quote]

Link to comment
Share on other sites

Dim $line, $HostFileName, $FileContent

$FileContent = ""

If @OSVersion = "WIN_XP" Then
$HostFileName = @SystemDir & "\Drivers\etc\hosts"

    $FileHandle = FileOpen($HostFileName, 0) ; 0 = open in read mode

    If Not $FileHandle Then
        MsgBox(4096, "Error", "Please contact your administrator - Hosts file could not be accessed")
        Exit
    EndIf

    ; Read in lines of text until the EOF is reached check if it has been configured already.
    While 1
        $line = FileReadLine($FileHandle)
        If @error = -1 Then ExitLoop
        $FileContent &=  $line & @CRLF ; Fixed the way you were appending too
    WEnd
    MsgBox(4096, "", $FileContent)
Else

    MsgBox(4096, "Fatal Error", "Unsupported OS, Script will run only on Windows XP")
    Exit

EndIfoÝ÷ Øx­é¬º1Á«rß}÷J­®'¶¬jëh×6#include <file.au3>
#include <Array.au3>

Dim $line, $HostFileName

IF @OSVersion = "WIN_XP" Then
    $HostFileName = "C:\Windows\System32\Drivers\etc\Hosts"
    _FileReadToArray ($HostFilename, $line)
    _ArrayDisplay ($line)
Else
    Msgbox(4096, "Fatal Error","Unsupported OS, Script will run only on Windows XP")
    Exit
EndIf

;-))

Stefan

Edited by 99ojo
Link to comment
Share on other sites

thanks a lot , u guys

both the versions are working i am gonna use the former one

but one thing, the trouble here is i wanted to find out why write mode wasnt working

i guess in write mode it would not show the file contents ??????

actually i am gonna do some entries into the hosts file and then if the entry already exists, it wil notify the user or else it wil go ahead with adding.

Link to comment
Share on other sites

i have had success modifying the script for adding local server ip to hosts file in our corporate network.

i am posting a modification which will enable local hosts entry if the pc is not properly configured with one

:-)

#Region;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_Comment=Developed with AutoIT v3.3.x
#AutoIt3Wrapper_Res_Description=Localhost Amendment tool (AutoIT 3.3.0)
#AutoIt3Wrapper_Res_Fileversion=1.1.0.0
#AutoIt3Wrapper_Res_LegalCopyright=vrr
#EndRegion;**** Directives created by AutoIt3Wrapper_GUI ****


#Region Includes, compiler directives
#include <_ErrorHandler.au3>;enables error handler
#include <Date.au3>
#EndRegion

#Region Product & version info, changelog
; AutoIt Version:   3.3.0.0
; Language:     English
; Platform:     WinXP
; Author:           Rajesh V R (rajeshwithsoftware at gmail dot com) 
; StartDate:        10 Mar 2009 
; LastEdited:       
; Script Function:  Checks user hosts fiel for localhost entry and creates if an entry is not found 
; File Version:     1.1
; VersionDate:    11 March 2009


; v1.1 <10 Mar 2009> code cleanup to suite template, coding suited to requirement now.
; v1.0 <10 Mar 2009> Started off, with forum users help, got code towork with hosts file!
#EndRegion

#Region Variable Declarations, etc
    Dim $line, $HostFileName, $FileContent
    Global $EntryTag = "NotFound"
    Global $AppTitle = "LocalHost Checker by vrr"
    Dim $MsgTitle = $AppTitle & " (" & _Now() & ")"
    $HostFileName = @SystemDir & "\Drivers\etc\hosts"
    $FileContent = ""
#EndRegion

#Region Main Function

    If @OSVersion = "WIN_XP" Then
        AnalyseHostsFile()
        AppendHostsFile()
        AnalyseHostsFile()
        
        If $EntryTag = "Exists" Then 
            $MsgString = "You PC is configured for proper access to localhost" 
        Else
            $MsgString = "Unable to amend Hosts file, Please contact your administrator "
        EndIf
    
    Else
        $MsgString = "Fatal Error - Unsupported OS, Application designed to run on Windows XP only"
    EndIf
    
    MsgBox(4096,$MsgTitle, $MsgSTring)
    
#EndRegion

#Region CustomFunctions

    Func AnalyseHostsFile()
        $FileHandle = FileOpen($HostFileName, 0); 0 = open in read mode

        If Not $FileHandle Then
            MsgBox(4096, $MsgTitle, "Please contact your administrator - Hosts file could not be accessed for reading")
            Exit
        EndIf

; Read in lines of text until the EOF is reached check if it has been configured already.
        While 1
            $line = FileReadLine($FileHandle)
            If @error = -1 Then ExitLoop
            $FileContent &=  $line & @CRLF; Fixed the way you were appending too
        WEnd
                
        IF StringInStr($FileContent,"127.0.0.1") = 0 Then 
            $EntryTag = "NotFound" 
        Else
            $EntryTag = "Exists"
        EndIf
        
        Fileclose($FileHandle)
        
    EndFunc


    Func AppendHostsFile()
        $FileHandle = FileOpen($HostFileName, 1); open for writing at the end of file
        
        If Not $FileHandle Then
            MsgBox(4096, $MsgTitle, "Please contact your administrator - Hosts file could not be accessed for editing")
            Exit
        EndIf

        If $EntryTag = "NotFound" Then 
            Msgbox (4096,$MsgTitle,"Your PC is currently not configured for localhost",1)
            FileWriteLine($FileHandle,"127.0.0.1 localhost")
        Endif 
        Fileclose($FileHandle)
    EndFunc

#EndRegion
Edited by rajeshontheweb
Link to comment
Share on other sites

  • 1 month later...

lol I didn't realize someone else was working on this too. ;)

Well, here's part of a large project I'm working on. It will work on any OS and finding the HOSTS file is simple.

In this example, it finds the HOSTS file for you. ^_^

$hosts = RegRead("HKEY_LOCAL_MACHINE\SYSTEM\ControlSet001\Services\Tcpip\Parameters", "DataBasePath") & "\HOSTS"

Please let me know how it works out for you. It worked perfectly fine on a Windows 7 machine.

By the way, any of you happen to have a script that checks all the IP addresses (netstat -n) and/or checks it against a site such as siteadvisor.com?

Edited by DJKMan

 

 

Link to comment
Share on other sites

thats nice, i normally use registry reading as last option just in case if there is any errors reading registry keys,i wont be able to access the hosts file path. but i do agree this is the most efficient way of doing, probably i should modify to use my option incase registry call fails ? - moreover , as we have mostly windows xp in our network, i wasnt really bothered about other OS :-) anyways, this will be helpful down the line in future...

and ur question? i dont get it fully, pls explain ( i am lost in another thought now probably) my code does check for a given ip address to see if already exists. in that place, u could check if siteadvisor.com exists in ur hosts file - if thats what u meant.... my bad. i am working on planning for such a possiblility already but i am currently concentrating on another couple of things, will come back if there is anything in this direction...

Edited by rajeshontheweb
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...