Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 09/22/2011 in all areas

  1. JScript

    UDF: _HostFile.au3

    Hello all, This functions is used for manipulate Host file located in %Systemroot%\System32\Drivers\Etc\Host. Current functions: ; =============================================================================================================================== _HostRead( [FilePath] ) _HostReadLine( LineNumber [, FilePath ] ) _HostWrite( IP, HostName [, OverWrite [, FilePath ]]) _HostDelete( IP, HostName [, FilePath ]) ; ===============================================================================================================================Send comments... #include-once #include <Array.au3> ; #INDEX# ======================================================================================================================= ; Title .........: _HostFile.au3 ; AutoIt Version.: 3.2.12++ ; Language.......: English ; Description ...: Functions that manipulate Host file located in %Systemroot%\System32\Drivers\Etc\Host. ; Author ........: João Carlos (jscript) ; =============================================================================================================================== ; #CURRENT# ===================================================================================================================== ;_HostRead ;_HostReadLine ;_HostWrite ;_HostDelete ; =============================================================================================================================== ; #INTERNAL_USE_ONLY#============================================================================================================ ;============================================================================================================================== ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostRead ; Description ...: Read all contents in the Host file. ; Syntax.........: _HostRead( [FilePath] ) ; Parameters ....: FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Sets @error = 0 and returns an two-dimensional array: ; $array[0][0] - Number of lines returned ; $array[1][0] - Line number ; $array[1][1] - IP number ; $array[1][2] - Host name ; ... ; $array[n][0] = nth Line number ; $array[n][1] = nth IP number ; $array[n][2] = nth Host name ; Failure - Returns the same two-dimensional array above with all values = 0 and: ; Sets @error = 1 if can not be open file ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; $aHost = _HostRead() ; =============================================================================================================================== Func _HostRead($FilePath = "") Local $FileOpen, $aFileLines, $nCount, $Line = "", $RedirectIP[1][3] = [[0, 0, 0]] If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(1, 0, $RedirectIP) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop ; $Line = StringSplit($Line, " " & Chr(9)) ;If @error Then $Line = StringSplit($Line, Chr(9)) If Not @error Then ReDim $RedirectIP[UBound($RedirectIP, 1) + 1][3] $RedirectIP[0][0] += 1 $RedirectIP[$RedirectIP[0][0]][0] = $nCount $RedirectIP[$RedirectIP[0][0]][1] = $Line[1] $RedirectIP[$RedirectIP[0][0]][2] = $Line[2] EndIf Next FileClose($FileOpen) Return $RedirectIP EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostReadLine ; Description ...: Read the specified line number in Host file. ; Syntax.........: _HostReadLine( LineNumber [, FilePath ] ) ; Parameters ....: LineNumber - Specified line number to be read. ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Sets @error = 0 and returns an one-dimensional with 2 elements: ; $array[0] - IP number ; $array[1] - Host name ; Failure - Sets @error = 1 and returns the same one-dimensional array above with 0 value. ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; $aHost = _HostReadLine($array[1][0]) ; =============================================================================================================================== Func _HostReadLine($LineNumber, $FilePath = "") Local $FileOpen, $aFileLines, $nCount, $Line = "", $RedirectIP[2] = [0, 0] If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(1, 0, $RedirectIP) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop $Line = StringSplit($Line, " ") If Not @error And $nCount = $LineNumber Then $RedirectIP[0] = $Line[1] $RedirectIP[1] = $Line[2] ExitLoop EndIf Next FileClose($FileOpen) Return $RedirectIP EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostWrite ; Description ...: Write redirections IP and HostName in the Host file. ; Syntax.........: _HostWrite( IP, HostName [, OverWrite [, FilePath ]]) ; Parameters ....: IP - IP addres. ; HostName - Host name. ; OvewrWrite - [optional] If set to 1, over write an existe value, the defaul is 0 ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Returns 1 ; Failure - Returns 0 and sets @error to: ; 1 - if file is readonly ; 2 - if file can not be open ; 3 - if not output file in write mode ; 4 - if file cannot be written ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; _HostWrite($IP, $HostName, 1) ; =============================================================================================================================== Func _HostWrite($IP, $HostName, $OverWrite = 0, $FilePath = "") Local $FileOpen, $aFileLines, $hWriteHandle, $nCount, $Line = "", $Flag = True, $Value0, $Value1 If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ; Check if file is readonly... If StringInstr(FileGetAttrib($FilePath), "R") Then Return SetError(1, 0, 0) ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(2, 0, 0) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF $aFileLines = StringSplit(StringStripCR($FileRead), @LF) ;Open the output file in write mode $hWriteHandle = FileOpen($FilePath, 2) If $hWriteHandle = -1 Then Return SetError(3, 0, 0) ;Loop through the array For $nCount = 1 To $aFileLines[0] $Line = StringStripWS($aFileLines[$nCount], 7) ; Skip comment lines proceeded by: # If StringLen($Line) < 10 Or StringRegExp(StringStripWS($Line, 8), "\A#") Then ContinueLoop $Value0 = StringRegExp($Line, "\b" & $IP & "\b") $Value1 = StringRegExp($Line, "\b" & $HostName & "\b") If ($Value0 And $Value1) Or ($OverWrite And ($Value0 Or $Value1)) Then $aFileLines[$nCount] = $IP & " " & $HostName $Flag = False ExitLoop EndIf Next If $Flag Then _ArrayInsert($aFileLines, $aFileLines[0], $IP & " " & $HostName) ;Write the lines back to original file. For $nCount = 1 To UBound($aFileLines) - 1 $Line = StringStripWS($aFileLines[$nCount], 7) If $Line = "" Then ContinueLoop ; If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then FileClose($hWriteHandle) Return SetError(4, 0, 0) EndIf Next FileClose($hWriteHandle) Return SetError(0, 0, 1) EndFunc ; #FUNCTION# ==================================================================================================================== ; Name...........: _HostDelete ; Description ...: Delete entries in the Host file. ; Syntax.........: _HostDelete( IP, HostName [, FilePath ]) ; Parameters ....: IP - IP addres. ; HostName - Host name. ; FilePath - [optional] The default location and name is %Systemroot%\System32\Drivers\Etc\Host. ; Return values .: Success - Returns 1 ; Failure - Returns 0 and sets @error to: ; 1 - if file is readonly ; 2 - if file can not be open ; 3 - if not output file in write mode ; 4 - if file cannot be written ; Author ........: jscript (João Carlos) ; Modified.......: ; Remarks .......: ; Related .......: ; Link ..........; ; Example .......; _HostDelete($IP, $HostName, 1) ; =============================================================================================================================== Func _HostDelete($IP = "", $HostName = "", $FilePath = "") Local $FileOpen, $aFileLines, $hWriteHandle, $nCount, $Line = "", $Flag = True If $FilePath = "" Then $FilePath = @SystemDir & "\Drivers\Etc\hosts" ; Check if file is readonly... If StringInstr(FileGetAttrib($FilePath), "R") Then Return SetError(1, 0, 0) ;Read the file into an array $FileOpen = FileOpen($FilePath, 0) If $FileOpen = -1 Then Return SetError(2, 0, 0) Local $FileRead = FileRead($FileOpen, FileGetSize($FilePath)) & @LF If ($IP <> "" And Not StringInStr($FileRead, $IP)) Or ($HostName <> "" And Not StringInStr($FileRead, $HostName)) Then _ Return SetError(3, 0, 0) $aFileLines = StringSplit(StringStripCR($FileRead), @LF) ;Open the output file in write mode $hWriteHandle = FileOpen($FilePath, 2) If $hWriteHandle = -1 Then Return SetError(4, 0, 0) ;Write the lines back to original file. For $nCount = 1 To UBound($aFileLines) - 1 $Line = StringStripWS($aFileLines[$nCount], 7) If $Line = "" Then ContinueLoop If $IP = "" And StringRegExp($Line, "\b" & $HostName & "\b") Then ContinueLoop If StringRegExp($Line, "\b" & $IP & "\b") And $HostName = "" Then ContinueLoop If StringRegExp($Line, "\b" & $IP & "\b") And StringRegExp($Line, "\b" & $HostName & "\b") Then ContinueLoop ; If FileWriteLine($hWriteHandle, $aFileLines[$nCount]) = 0 Then FileClose($hWriteHandle) Return SetError(5, 0, 0) EndIf Next FileClose($hWriteHandle) Return SetError(0, 0, 1) EndFunc@shai, Thank you for having found a mistake! Regards, João Carlos.
    1 point
  2. GEOSoft

    Open hosts file ?

    What version of AutoIt are you using? Very old can be decompiled using the Exe2Aut.exe file Did you tell The AutoIt wrapper to save the script inside the EXE? That makes it simple to get the code. Main point: Any Code in Any Language can be decompiled if you have the interest in doing it and if you think there is some benefit to be gained that is worth the effort.
    1 point
  3. Bert

    Open hosts file ?

    For such a simple thing why do you want to keep your code from being decompiled? That makes no sense seeing: 1. You posted your code already 2. Your basically did in your code create a GUI with a button to open the host file in notepad. Hardly anyone would waste time trying to decompile something as simple as that. If anything they would just write something to do the job.
    1 point
  4. rover

    Simple WinPE 3.x Shell

    Here's a variation on the code in this post by Rasim http://www.autoitscript.com/forum/index.php?showtopic=85912&hl=popup This will run on winpe. You can block the popup menu over the winpe taskbar (AutoIt gui?) with WindowFromPoint. Edit: I guess at some point the code tags will get sorted out (again) from the damage IP.Board does with each of their 'upgrades'. In the mean time, we at least have Tidy in SciTE #include <GuiConstantsEx.au3> #include <WindowsConstants.au3> #include <GuiMenu.au3> #include <Misc.au3> OnAutoItExitRegister("_Exit") Opt('MustDeclareVars', 1) Opt("TrayAutoPause", 0) Global $sTaskTitle = "au3 taskbar title" Global $hGUI, $hMenu, $aPos, $hWin Global $iRun1, $iRun2, $iRun3 Global $tStruct = DllStructCreate($tagPoint) Global $hDLL = DllOpen("User32.dll") $hGUI = GUICreate("", 1, 1, -100, -100, $WS_POPUP, $WS_EX_TOOLWINDOW) Global $cMenu = GUICtrlCreateContextMenu() $hMenu = GUICtrlGetHandle(-1) $iRun1 = GUICtrlCreateMenuItem("Run 1", $cMenu) GUICtrlCreateMenuItem("", $cMenu) $iRun2 = GUICtrlCreateMenuItem("Run 2", $cMenu) GUICtrlCreateMenuItem("", $cMenu) $iRun3 = GUICtrlCreateMenuItem("Run 3", $cMenu) While 1 Sleep(20) If _IsPressed("02", $hDLL) Then $aPos = MouseGetPos() DllStructSetData($tStruct, "x", $aPos[0]) DllStructSetData($tStruct, "y", $aPos[1]) $hWin = _WinAPI_WindowFromPoint($tStruct) If WinGetTitle($hWin) = $sTaskTitle Then ContinueLoop WinMove($hGUI, "", $aPos[0], $aPos[1]) GUISetState(@SW_SHOW, $hGUI) Switch _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI, $aPos[0], $aPos[1], 1, 1, 2) Case $iRun1 MsgBox(4096, "Run 1", "") Case $iRun2 MsgBox(4096, "Run 2", "") Case $iRun3 MsgBox(4096, "Run 3", "") EndSwitch GUISetState(@SW_HIDE, $hGUI) EndIf WEnd Func _Exit() GUIDelete($hGUI) DllClose($hDLL) _GUICtrlMenu_DestroyMenu($hMenu) EndFunc ;==>_Exit
    1 point
  5. GEOSoft

    Open hosts file ?

    One of my very first released AutoIt Scripts was a HOST file manager, so yes; there are legitimate reasons for working with the host file. @MrVietA2 You use FileOpen(), File Read(), FileClose() the same way for Host as you do for any other file and all the help required for that is in the Help file. For simply reading it you don't need FileOpen() at all; just go with FileRead(). There are also not so legit reasons for messing with that file and if you get caught doing any of those it won't be a nice outcome. Understandably people are suspicious when you start playing with certain files. Of course the easiest way for people to avoid problems with HOST is to keep it regularly updated and have a look at the checksum periodically.
    1 point
  6. Bert

    Open hosts file ?

    Why are you so intent on editing your host file with an AutoIt script? No, we won't give you "personal" service with instant messenger. You need to post your code and explain what you have in mind. Saying "I want to open my HOST file isn't enough. WHY you want to do it and the purpose of doing it would explain much.
    1 point
  7. JScript

    Open hosts file ?

    Simple: ShellExecute(@SystemDir & "\Notepad.exe", @SystemDir & "\Drivers\Etc\hosts", "") João Carlos.
    1 point
×
×
  • Create New...