Sign in to follow this  
Followers 0
MrVietA2

Open hosts file ?

26 posts in this topic

Hi everyone !

I have a 1 question : " How can I open hosts file in windows with autoit code ? ". I tried by some other program but failed ! Help me plz ! Thank you ! :graduated:

Share this post


Link to post
Share on other sites



Hi, try this:

João Carlos.

I include the au3 file and use _HostRead("C:\Windows\System32\drivers\etc\hosts") but it doesn't work . Help me, thanks

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

You must to be use this directive: #RequireAdmin

Compile your script and see the result!

João Carlos.

I try but it doesn't work, can you help me on Yahoo ?

Y!M : destinystars_neverstop@yahoo.com

Thanks !

Edited by MrVietA2

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

Doubts only here, and the UDF was only tested on XP!

João Carlos.

My OS is windows 7 :graduated:, what else ? ;) How can I do that ?

Edited by MrVietA2

Share this post


Link to post
Share on other sites

#8 ·  Posted (edited)

My OS is Win7 and it works perfectly!

#RequireAdmin

 _HostWrite("127.0.0.0", "Computer", 1)

$asHost = _HostRead()
_ArrayDisplay($asHost, "HostFile")

_HostDelete("127.0.0.0", "Computer")

$asHost = _HostRead()
_ArrayDisplay($asHost, "HostFile")

João Carlos.

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

That is use like a box, but i want to compile my script , run and like this :

Posted Image

Share this post


Link to post
Share on other sites

#10 ·  Posted (edited)

Inside the source of the UDF, you will see these lines:

;
; Skip comment lines proceeded by: #
If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop
;
Your hosts file only have comments?

João Carlos.

Edited by jscript

http://forum.autoitbrasil.com/ (AutoIt v3 Brazil!!!)

Somewhere Out ThereJames Ingram

somewh10.png

dropbo10.pngDownload Dropbox - Simplify your life!
Your virtual HD wherever you go, anywhere!

Share this post


Link to post
Share on other sites

Inside the source of the UDF, you will see these lines:

;
; Skip comment lines proceeded by: #
If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop
;
Your hosts file only have comments?

João Carlos.

I don't understand , i want to open hosts file with notepad like #9, can you help me ? Thanks

Share this post


Link to post
Share on other sites

Simple:

ShellExecute(@SystemDir & "\Notepad.exe", @SystemDir & "\Drivers\Etc\hosts", "")

João Carlos.

Oh, it work ! Thanks !

Can use :

#include <Process.au3>

_RunDOS("start %windir%\notepad.exe %windir%\system32\drivers\etc\hosts")

Share this post


Link to post
Share on other sites

Why would you use _RunDOS over Run or ShellExectute? No includes needed with either of the others, and _RunDOS should only be used for things that won't work any other way.


If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.  -  ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.  -  Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.  -  _FileGetProperty - Retrieve the properties of a file  -  SciTE Toolbar - A toolbar demo for use with the SciTE editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Share this post


Link to post
Share on other sites

#16 ·  Posted (edited)

I have an other question : " How can I make a hosts file with FileOpen() ? " . Thanks so much

Edited by MrVietA2

Share this post


Link to post
Share on other sites

Why are you so intent on editing your host file with an AutoIt script? No, we won't give you "personal" service with instant messenger.

You need to post your code and explain what you have in mind. Saying "I want to open my HOST file isn't enough. WHY you want to do it and the purpose of doing it would explain much.

1 person likes this

Share this post


Link to post
Share on other sites

One of my very first released AutoIt Scripts was a HOST file manager, so yes; there are legitimate reasons for working with the host file.

@MrVietA2

You use FileOpen(), File Read(), FileClose() the same way for Host as you do for any other file and all the help required for that is in the Help file. For simply reading it you don't need FileOpen() at all; just go with FileRead().

There are also not so legit reasons for messing with that file and if you get caught doing any of those it won't be a nice outcome. Understandably people are suspicious when you start playing with certain files. Of course the easiest way for people to avoid problems with HOST is to keep it regularly updated and have a look at the checksum periodically.

1 person likes this

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Share this post


Link to post
Share on other sites

#19 ·  Posted (edited)

Why are you so intent on editing your host file with an AutoIt script? No, we won't give you "personal" service with instant messenger.

You need to post your code and explain what you have in mind. Saying "I want to open my HOST file isn't enough. WHY you want to do it and the purpose of doing it would explain much.

I only want to open hosts file and want user can edit it :graduated:, nothing else ;) !

This is my code :

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
 
$Form1 = GUICreate("Open hosts file !", 316, 130, 407, 229)
$Label1 = GUICtrlCreateLabel("OPEN THE HOSTS FILE IN WINDOWS", 16, 24, 285, 23)
GUICtrlSetFont(-1, 12, 800, 0, "Times New Roman")
GUICtrlSetColor(-1, 0x008000)
$Button1 = GUICtrlCreateButton("OPEN", 80, 56, 153, 57)
GUICtrlSetFont(-1, 11, 800, 0, "Times New Roman")
GUISetState(@SW_SHOW)
 
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Button1
            _open()
    EndSwitch
WEnd
 
Func _open()
ShellExecute(@SystemDir & "\Notepad.exe", @SystemDir & "\Drivers\Etc\hosts", "")
EndFunc

@GEOsoft : I know how to do this :), thanks for that ;)

THANKS ALL

Edited by MrVietA2

Share this post


Link to post
Share on other sites

#20 ·  Posted (edited)

I compile my script to exe file but it can decompile . I have 1 question : " How can I protect my source not decompile ? ". Thanks :graduated:

P/s : My English is bad, sorry about that ;)

Edited by MrVietA2

Share this post


Link to post
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
Sign in to follow this  
Followers 0

  • Similar Content

    • stafe
      By stafe
      I want to write a script to toggle the opening and closing of a pdf file.
      For example if i have a flie called myFile.pdf I want the script to open/load it.  However if myFile.pdf is open then i want the same script to close it. 
      Could someone please tell me how I can know if the pdf file is open/loaded?
      Thank you 
      Simon
    • Jefrey
      By Jefrey
      Events-based UDF to help on handling one or more TCP connections
    • Jefrey
      By Jefrey
      Hi guys!
      I'm having a trouble with TCPAccept() on compiled scripts.
      This script I made to test:
      #include <Debug.au3> _DebugSetup() TCPStartup() $Main = TCPListen("0.0.0.0", 8081) _DebugOut("TCPListen output: " & $Main) While True $ac = TCPAccept($Main) _DebugOut("TCPAccept return: " & $ac) If $ac <> -1 Then _DebugOut("There's someone out here!") TCPSend($ac, "hi") EndIf WEnd Works fine if I run it by pressing F5 on SciTe window.

      But if I compile it and then execute the executable file, it does not work. Instead, NetCat tells me that the connection timed out (I've tested other clients as well):

      I've checked if the port is really opened, if I really can open it, if there is other software using that port, but everything showed that it should connect.
      Btw, netstat -an shows that the port is really listening (when I execute my file). It listens, but does not accept.

      I use no antivirus software (I use this Windows installation for coding only) and Windows Firewall is disabled. I've tried, anyway, allowing my exe file on Firewall rules, but it also did not work. Running as administrator also did not help.
      I believe it's a Windows (Firewall?) bug rather than an AutoIt bug. How can I manage to solve this?
      Thanks in advance.
       
    • JohnOne
      By JohnOne
      I cannot find any concrete info on this.
      What is and is not valid in a windows (vista+) hosts file?
      0.0.0.0 http://www.domain.com (I know for example that http:// is not valid)
      0,0,0,0 www.domain.com 0.0.0.0 sub.domain.com 0.0.0.0 sub.sub.domain.com 0.0.0.0 domain_name.com 0.0.0.0 domain-mane.com 0.0.0.0 1domain.com 0.0.0.0 _domain.co 0.0.0.0 2
    • EvoBora
      By EvoBora
      Hello Everyone, I'm running into an issue with the below hosts file script originally posted by Chewy.  Has the Author as Matt Majewski back in 2009.  Not sure if it was edited from the original creation date.  The problem I'm running into is that it is not editing the hosts file it is just appending the line to the hosts file over and over again.  It seems like it can't find the name when it searches the array.  Does someone have a working script that allows you to edit an ip if it exists or add it if it doesn't?  I have tried this on multiple computers all running Win7 Pro 64bit.  Any help would greatly be appreciated.  I'm sure I'm just missing something, but I can't seem to see it.
      Thank You
      Func HostUpdate() ;EDIT IP TO THE IP ADDRESS OF YOUR SERVER YOU WANT MACHINES TO IMPORT TO $CitrixIP = "10.99.1.1" ;HOSTS file path $sFile = @WindowsDir & "\system32\Drivers\etc\hosts" ;used to define the index in the array when citrix.mydomain.com was found Dim $CitrixFind ;the array used to import the HOSTS file to Dim $aRecords ;Open the HOSTS file for reading into the array ;If Not _FileReadToArray(@SystemDir & "\Drivers\etc\hosts",$aRecords) Then If Not _FileReadToArray($sFile, $aRecords) Then MsgBox(4096,"Error", " Error reading log to Array error:" & @error) ;MsgBox(4096,"Error", $sFile) Exit EndIf ;searches the imported array to find the word "citrix.mydomain.com" ;then set the arrayindex it found it into $CitrixFind ;$CitrixFind = _ArraySearch ($aRecords, "citrix.mydomain.com", 0 , 0, 0, True) $CitrixFind = _ArraySearch ($aRecords, "citrix.mydomain.com") ;IF it found citrix.mydomain.com IF ($CitrixFind <> -1) Then ;change the array to include the new ip Number ;it changes the specific array index based on the earlier search $aRecords[$CitrixFind] = $CitrixIP & " citrix.mydomain.com" ;open the host file for editing ;the 2 parameter will erase the contents of the file $hFile = FileOpen($sFile, 2); 2 = erase ;write the array to the open File _FileWriteFromArray($hfile, $aRecords, 1) ;close the HOSTS file that was opened FileClose($hFile) ; did not find citrix.mydomain.com Else ;open the host file for editing ;the 1 parameter will append to the end of the file $hFile = FileOpen($sFile, 1); 1 = append ;write the new zen ip to the end of the file FileWriteLine($hFile, $CitrixIP & " citrix.mydomain.com") ;close the HOSTS file that was opened Fileclose($hFile) ;END IF found citrix.mydomain.com of not EndIf EndFunc