modify host file with autoit

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

bbimisAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

JohnBusiness Consultant (Owner)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.
0
bbimisAuthor 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?
0
JohnBusiness Consultant (Owner)Commented:
Yes, the change is fine. But you need to be administrator to make the change.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

bbimisAuthor Commented:
of course but that doesn't solve the code question I had.
0
JohnBusiness Consultant (Owner)Commented:
The format you are using / showing is fine.

What issue do you have?  What you are showing should work.
0
bbimisAuthor 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).
0
JohnBusiness Consultant (Owner)Commented:
That must be a flaw in autoit. I have not used this. I am sure it is not a Windows limitation.
0
bbimisAuthor 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.
0
Bill PrewCommented:
Do you have to have a solution using AutoIt, or could some other tool like VBS work?

~bp
0
bbimisAuthor Commented:
any tool can work. doesn't matter just a working solution is fine. vbs is fine too.
0
bbimisAuthor 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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bbimisAuthor Commented:
working solution
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Scripting Languages

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.