; ---------------------------------------------------------------------------- ; ; AutoIt Version: 3.0.103 ; Language: English ; Platform: Win9x / NT ; Author: HUCA ; ; Script Function: ; Add DNS names into the system host file ; ; ---------------------------------------------------------------------------- #include #include ; ---------------------------------------------------------------------------- ; Set up our defaults ; ---------------------------------------------------------------------------- AutoItSetOption("MustDeclareVars", 1) AutoItSetOption("MouseCoordMode", 0) AutoItSetOption("PixelCoordMode", 0) AutoItSetOption("RunErrorsFatal", 0) AutoItSetOption("TrayIconDebug", 1) AutoItSetOption("WinTitleMatchMode", 4) AutoItSetOption("TrayIconHide", 1) ; ---------------------------------------------------------------------------- ; Script Start ; ---------------------------------------------------------------------------- ;======== Main ===============> Dim $i Dim $FileName = "hosts" Dim $hostList[2] $hostList[0]="192.168.1.1 server1.local.lan server1" $hostList[1]="192.168.1.2 server2.local.lan server2" chOSv () For $i = 0 to 5 If findIt ($FileName, $hostList[$i]) Then addThis ($FileName, $hostList[$i]) EndIf Next MsgBox (0,"hosts", "File was modified successfully") ;======== End Main ===============> ;======== Start Functions ===============> ;======== check version of OS ===============> Func chOSv () Local $Ver Local $98Dir = @WindowsDir Local $NTDir = @SystemDir & "\drivers\etc" Local $2kDir = @SystemDir & "\drivers\etc" $Ver = @OSVersion Select Case $ver = "WIN_95" FileChangeDir($98Dir) Case $ver = "WIN_98" FileChangeDir($98Dir) Case $ver = "WIN_ME" FileChangeDir($98Dir) Case $ver = "WIN_NT4" FileChangeDir($NTDir) Case $ver = "WIN_2000" FileChangeDir($2kDir) Case $ver = "WIN_XP" FileChangeDir($2kDir) Case Else MsgBox(0, "Error", "Operating System - Not recognized!") Exit EndSelect EndFunc ;======== check if the line exist ===============> Func findIt ($FileName, $hostLine) Local $File Local $line $File = FileOpen($FileName, 0) If $File = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf while 1 $line = FileReadLine($File) If $line == $hostLine Then FileClose($File) return 0 ExitLoop EndIf If @error = -1 Then ExitLoop WEnd FileClose($File) return 1 EndFunc ;======== add line into file ===============> Func addThis ($FileName, $line) Local $File $File = FileOpen($FileName, 1) If $File = -1 Then MsgBox(0, "Error", "Unable to open file.") Exit EndIf FileWriteLine($File, @CRLF & $line) FileClose($File) EndFunc ;======== End Functions ===============>