modify host file with autoit

bbimis
bbimis used Ask the Experts™
on
have some servers that i need to modify the host file on if they can't resolve. would like to do it with autoit
Basically i want to check to see if say "myserver.domain.com" is pinging and if it isn't then change/add entry to host file to the correct ip address.

So if its currently 12.12.12.1   i want to search the host file and comment out the line for 12.12.12.1 if it exist and change it to 10.10.10.1 (obviously the ips are examples).

i have this so far but not sure how to parse the host file and change existing entries. thanks!
changehost()

Func changehost()
    ; Ping myserver.domain.com with a timeout of 250ms.
    Local $iPing = Ping("myserver.domain.com", 250)
    Local $ipaddress = TCPNameToIP("myserver.domain.com")
	$Server = "myserver.domain.com"
    If $iPing Then ; If a value greater than 0 then all is good..

	 Else ; we need to change the host entry
		 if $ipaddress = "12.12.12.1" Then
			$OPENHOSTS = FileOpen("C:\WINDOWS\system32\drivers\etc\hosts", 1)
			FileWrite($OPENHOSTS, "10.10.10.1" & @TAB & $Server & @CRLF)
			FileClose($OPENHOSTS)
			; write to host file 10.10.10.1 and remove entry with 12.12.12.1 this is where i'm confused on how to do that
		 Else
			$OPENHOSTS = FileOpen("C:\WINDOWS\system32\drivers\etc\hosts", 1)
			FileWrite($OPENHOSTS, "12.12.12.1" & @TAB & $Server & @CRLF)
			FileClose($OPENHOSTS)
			;write to the host file 12.12.12.1 and remove 10.10.10.1 entry
		 EndIf
        
    EndIf
 EndFunc   ;==>changehost

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
It looks like it should work (format looks correct). The hosts file is mostly comments and the active lines can normally be in any order.

Is it working?  You do need admin authority to changes HOSTS.  Your program (or Notepad) needs to Run as Administrator to change that file.

Author

Commented:
well it works per say but it needs to do the following and not sure how
say hostfile has this in it
10.10.10.1  myserver.domain.com


my current code would make the host file look like this
10.10.10.1  myserver.domain.com
12.12.12.1  myserver.domain.com

where I want to instead find the entry and overwrite it so it looks like this
12.12.12.1 myserver.domain.com

may sense?
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
Yes, the change is fine. But you need to be administrator to make the change.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
of course but that doesn't solve the code question I had.
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
The format you are using / showing is fine.

What issue do you have?  What you are showing should work.

Author

Commented:
as I mentioned it takes and appends the to the host file
I want it to instead of appending do a search for existing server entries for myserver.domain.com
and then change it accordingly

currently If I have a populated host file of say
1.2.3.4 somesite.com
2.4.5.6 another site
10.10.10.1 myserver.domain.com

I want it to take and change the instance of 10.10.10.1 accordingly
instead it takes and appends it to the bottom so it results in two different entries for the same address (not good).
JohnBusiness Consultant (Owner)
Most Valuable Expert 2012
Expert of the Year 2018

Commented:
That must be a flaw in autoit. I have not used this. I am sure it is not a Windows limitation.

Author

Commented:
anyone else wanna help with this and no flaw.
what i'm trying to do is this but i can't get my logic correct.
if $ipaddress = "12.12.12.1" Then
			$HostFileName = @SystemDir & "\Drivers\etc\hosts"
			Local $FileRead = FileReadline($hostfilename)
			If StringinStr($FileRead, "#10.10.10.1 myserver.domain.com")> 0 then
			   StringReplace("#10.10.10.1 myserver.domain.com","","10.10.10.1 myserver.domain.com")
			   else
			   $OPENHOSTS = FileOpen("C:\WINDOWS\system32\drivers\etc\hosts", 1)
			   FileWrite($OPENHOSTS, "10.10.10.1" & @TAB & $Server & @CRLF)
			   FileClose($OPENHOSTS)
			EndIf

		 Else
			If StringinStr($FileRead, "#12.12.12.1 myserver.domain.com")> 0 then
			   StringReplace("#12.12.12.1 myserver.domain.com","","12.12.12.1 myserver.domain.com")
			   else
			   $OPENHOSTS = FileOpen("C:\WINDOWS\system32\drivers\etc\hosts", 1)
			   FileWrite($OPENHOSTS, "70.167.48.216" & @TAB & $Server & @CRLF)
			   FileClose($OPENHOSTS)
			EndIf

Open in new window


and it doesn't even have to but autoit just some code rather it be batch, vbs i don't care just need to check and change.
Bill PrewIT / Software Engineering Consultant
Top Expert 2016

Commented:
Do you have to have a solution using AutoIt, or could some other tool like VBS work?

~bp

Author

Commented:
any tool can work. doesn't matter just a working solution is fine. vbs is fine too.
Commented:
figured it out for anyone interested.
#cs
This code will take and ping example.domain.com
if it doesn't resolve then it will change the host file automatically

#ce
#include <MsgBoxConstants.au3>
#include <FileConstants.au3>



changehost()

Func changehost()
   ; Start the TCP service.
    TCPStartup()

    ; Ping example.domain.com with a timeout of 250ms.
    Local $iPing = Ping("example.domain.com", 250)
    Local $ipaddress = TCPNameToIP("example.domain.com")
	;MsgBox(1,"example.domain.com ip address is",$ipaddress)

	Global $szfile = @SystemDir &"\drivers\etc\hosts"
	Global $sztxt = FileRead($szfile,FileGetSize($szfile))



    ; Register OnAutoItExit to be called when the script is closed.
    OnAutoItExitRegister("OnAutoItExit")

   If $iPing > 0 Then ; If a value greater than 0 then all is good..

   Else ; we need to change the host entry
		 if $ipaddress = "put an ip address here you want" Then
			;MsgBox(1,"ip being changed to ","put an ip address here you want")
			$sztxt = StringReplace($sztxt,$ipaddress,"put an ip address here you want")
			FileDelete($szfile)
			;MsgBox(1,"szfile is",$szfile)
			FileWrite($szfile,$sztxt)
		 elseif $ipaddress = "put an ip address here you want" Then
			$sztxt = StringReplace($sztxt,$ipaddress,"put an ip address here you want")
			FileDelete($szfile)
			FileWrite($szfile,$sztxt)
			;MsgBox(1,"ip changed to","put an ip address here you want")
		 EndIf


   EndIf

 EndFunc   ;==>changehost

Func OnAutoItExit()
    TCPShutdown() ; Close the TCP service.

Open in new window

Author

Commented:
working solution

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial