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
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).
Is it working? You do need admin authority to changes HOSTS. Your program (or Notepad) needs to Run as Administrator to change that file.