Avatar of bbimis
bbimis
 asked on

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

Scripting LanguagesProgrammingProgramming Languages-Other

Avatar of undefined
Last Comment
bbimis

8/22/2022 - Mon
John

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.
bbimis

ASKER
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?
John

Yes, the change is fine. But you need to be administrator to make the change.
This is the best money I have ever spent. I cannot not tell you how many times these folks have saved my bacon. I learn so much from the contributors.
rwheeler23
bbimis

ASKER
of course but that doesn't solve the code question I had.
John

The format you are using / showing is fine.

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

ASKER
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).
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
John

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

ASKER
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 Prew

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

~bp
Your help has saved me hundreds of hours of internet surfing.
fblack61
bbimis

ASKER
any tool can work. doesn't matter just a working solution is fine. vbs is fine too.
ASKER CERTIFIED SOLUTION
bbimis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bbimis

ASKER
working solution