Jump to content

(Solved) Blocking Address via Hosts file


PHAK
 Share

Recommended Posts

  • Moderators

"did not help" doesn't give us much to go on, right? Does it not write to the file? Does it write to the file but not block the address? Do you get an error?

Also, as there thread you mention has several suggestions in it, how about posting the code you are using specifically, so we can test?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

8 minutes ago, JLogan3o13 said:

"did not help" doesn't give us much to go on, right? Does it not write to the file? Does it write to the file but not block the address? Do you get an error?

Also, as there thread you mention has several suggestions in it, how about posting the code you are using specifically, so we can test?

Sorry

#include <array.au3>
#include <file.au3>

Dim $s_FileInputPath = "hosts"
Dim $s_FileOutputPath = "hosts2";for you this will be the same as the input
Dim $i_InserLineNumber = 25
Dim $s_InserLineContent = "127.0.0.1 www.facebook.com"


;A return value 1 indicates success.  A return value of 0 or less indicates failure.
WriteLineToFile($s_FileInputPath ,$s_FileOutputPath ,$i_InserLineNumber,$s_InserLineContent)

Func WriteLineToFile($s_FileName,$s_FileOutputName,$i_LineNumber,$s_Line)
Local $as_HostsContent
If Not _FileReadToArray($s_FileName,$as_HostsContent) Then Return 0
;~ _ArrayDisplay($as_HostsContent)

;This if statement extends the number of lines in the file if the line that is being inserted to is currently larger than the total number of lines in the file
If UBound($as_HostsContent) < $i_LineNumber  Then
    ReDim $as_HostsContent[$i_LineNumber+1]
EndIf

If Not _ArrayInsert($as_HostsContent,$i_LineNumber,$s_Line) Then Return -1
;~ _ArrayDisplay($as_HostsContent)

If Not _FileWriteFromArray($s_FileOutputName,$as_HostsContent,1) Then Return -2
Return 1
EndFunc

+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
+>23:05:24 AutoIt3.exe ended.rc:0
+>23:05:24 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 2.754

Thanks

Link to comment
Share on other sites

  • Moderators

You still didn't answer my question. Is the problem that it does not add the address to the Hosts file at all? Or does it add to the file and you can still get to the address?

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

3 minutes ago, JLogan3o13 said:

You still didn't answer my question. Is the problem that it does not add the address to the Hosts file at all? Or does it add to the file and you can still get to the address?

 it does not add the address to the Hosts file at all

Link to comment
Share on other sites

  • Developers
3 minutes ago, PHAK said:

Hosts

correct ...it will add it in the hosts2 file in the script directory.
Honestly, this script is quite messy and wat too complex... but anyway, check that file.

Jos

SciTE4AutoIt3 Full installer Download page   - Beta files       Read before posting     How to post scriptsource   Forum etiquette  Forum Rules 
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Link to comment
Share on other sites

2 minutes ago, Jos said:

correct ...it will add it in the hosts2 file in the script directory.
Honestly, this script is quite messy and wat too complex... but anyway, check that file.

Jos

Thanks
I took it from here
Do you have anything else?
Or arrange it

Link to comment
Share on other sites

@Exit

I tried to do that from the beginning
And failed

 it does not add the address to the Hosts file at all

#include <array.au3>
#include <file.au3>

Dim $s_FileInputPath = @SystemDir & "\drivers\etc\hosts"
Dim $s_FileOutputPath = @SystemDir & "\drivers\etc\hosts"
Dim $i_InserLineNumber = 25
Dim $s_InserLineContent = "127.0.0.1 www.facebook.com"


;A return value 1 indicates success.  A return value of 0 or less indicates failure.
WriteLineToFile($s_FileInputPath ,$s_FileOutputPath ,$i_InserLineNumber,$s_InserLineContent)

Func WriteLineToFile($s_FileName,$s_FileOutputName,$i_LineNumber,$s_Line)
Local $as_HostsContent
If Not _FileReadToArray($s_FileName,$as_HostsContent) Then Return 0
;~ _ArrayDisplay($as_HostsContent)

;This if statement extends the number of lines in the file if the line that is being inserted to is currently larger than the total number of lines in the file
If UBound($as_HostsContent) < $i_LineNumber  Then
    ReDim $as_HostsContent[$i_LineNumber+1]
EndIf

If Not _ArrayInsert($as_HostsContent,$i_LineNumber,$s_Line) Then Return -1
;~ _ArrayDisplay($as_HostsContent)

If Not _FileWriteFromArray($s_FileOutputName,$as_HostsContent,1) Then Return -2
Return 1
EndFunc

 

Edited by PHAK
Link to comment
Share on other sites

@Nine

I'm new to the field

Hope That I did well

Did not succeed

 it does not add the address to the Hosts file at all

 

 

#RequireAdmin
#include <array.au3>
#include <file.au3>
#include <WinAPIFiles.au3>
_WinAPI_Wow64EnableWow64FsRedirection (False)

Dim $s_FileInputPath = @SystemDir & "\drivers\etc\hosts"
Dim $s_FileOutputPath = @SystemDir & "\drivers\etc\hosts"
Dim $i_InserLineNumber = 25
Dim $s_InserLineContent = "127.0.0.1 www.facebook.com"


;A return value 1 indicates success.  A return value of 0 or less indicates failure.
WriteLineToFile($s_FileInputPath ,$s_FileOutputPath ,$i_InserLineNumber,$s_InserLineContent)

Func WriteLineToFile($s_FileName,$s_FileOutputName,$i_LineNumber,$s_Line)
Local $as_HostsContent
If Not _FileReadToArray($s_FileName,$as_HostsContent) Then Return 0
;~ _ArrayDisplay($as_HostsContent)

;This if statement extends the number of lines in the file if the line that is being inserted to is currently larger than the total number of lines in the file
If UBound($as_HostsContent) < $i_LineNumber  Then
    ReDim $as_HostsContent[$i_LineNumber+1]
EndIf

If Not _ArrayInsert($as_HostsContent,$i_LineNumber,$s_Line) Then Return -1
;~ _ArrayDisplay($as_HostsContent)

If Not _FileWriteFromArray($s_FileOutputName,$as_HostsContent,1) Then Return -2
Return 1
EndFunc

 

Link to comment
Share on other sites

; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"
If Not StringInStr(FileRead($sHostsFile), $sURL) Then FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & @TAB & $sURL & @CRLF)
ShellExecute("Notepad.exe", $sHostsFile)

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

4 minutes ago, Exit said:
; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"
If Not StringInStr(FileRead($sHostsFile), $sURL) Then FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & @TAB & $sURL & @CRLF)
ShellExecute("Notepad.exe", $sHostsFile)

 

Thanks
But fails
Just opens Notepad
And that's it

Link to comment
Share on other sites

Can you please run the following code and post the complete console log in a spoiler.

; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"

$sText = FileRead($sHostsFile)
ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)

If StringInStr($sText, $sURL) Then
    ConsoleWrite("URL already present" & @CRLF)
Else
    $rc = FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & @TAB & $sURL & @CRLF)
    ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & " RC: " & $rc & @LF)
EndIf

$sText = FileRead($sHostsFile)
ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)

ShellExecute("Notepad.exe", $sHostsFile)

 

Edited by Exit

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

8 minutes ago, Exit said:

Can you please run the following code and post the complete console log in a spoiler.

; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"

$sText = FileRead($sHostsFile)
ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)

If StringInStr($sText, $sURL) Then
    ConsoleWrite("URL already present" & @CRLF)
Else
    $rc = FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & @TAB & $sURL & @CRLF)
    ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & " RC: " & $rc & @LF)
EndIf

$sText = FileRead($sHostsFile)
ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)

ShellExecute("Notepad.exe", $sHostsFile)

 

 

Spoiler

>"C:\Program Files (x86)\AutoIt3\SciTE\..\AutoIt3.exe" "C:\Program Files (x86)\AutoIt3\SciTE\AutoIt3Wrapper\AutoIt3Wrapper.au3" /run /prod /ErrorStdOut /in "C:\11.au3" /UserParams    
+>02:33:06 Starting AutoIt3Wrapper (19.1127.1402.0} from:SciTE.exe (4.2.0.0)  Keyboard:0002040D  OS:WIN_10/  CPU:X64 OS:X64  Environment(Language:040D)  CodePage:0  utf8.auto.check:4
+>         SciTEDir => C:\Program Files (x86)\AutoIt3\SciTE   UserDir => C:\Users\PC\AppData\Local\AutoIt v3\SciTE\AutoIt3Wrapper   SCITE_USERHOME => C:\Users\PC\AppData\Local\AutoIt v3\SciTE 
>Running AU3Check (3.3.14.5)  from:C:\Program Files (x86)\AutoIt3  input:C:\11.au3
+>02:33:06 AU3Check ended.rc:0
+>Setting Hotkeys...--> Press Ctrl+Alt+Break to Restart or Ctrl+BREAK to Stop.
->02:33:06 Script requires Admin rights while SciTE is at normal level -> Starting new AutoIt3Wrapper with #RequireAdmin to run the script.
+>02:33:10 Script started, waiting for output. (pid=3976)
-##>02:33:10 Restarted AutoIt3Wrapper with #RequireAdmin. (pid=3976)
>Running:(3.3.14.5):C:\Program Files (x86)\AutoIt3\autoit3.exe "C:\11.au3"     
Error: 0 Extended: 1229 Line: 7

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost

0.0.0.0 activation.easeus.com
0.0.0.0 update.easeus.com
0.0.0.0 track.easeus.com
0.0.0.0 www.easeus.com
0.0.0.0 activation.acronis.com
0.0.0.0 web-api-tih.acronis.com
127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 na1r.services.adobe.com
127.0.0.1 hlrcv.stage.adobe.com
127.0.0.1 practivate.adobe.com
127.0.0.1 genuine.adobe.com
127.0.0.1 prod.adobegenuine.com

Error: 0 Extended: 0 Line: 13 RC: 0
Error: 0 Extended: 1229 Line: 17

# Copyright (c) 1993-2009 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

# localhost name resolution is handled within DNS itself.
#    127.0.0.1       localhost
#    ::1             localhost

0.0.0.0 activation.easeus.com
0.0.0.0 update.easeus.com
0.0.0.0 track.easeus.com
0.0.0.0 www.easeus.com
0.0.0.0 activation.acronis.com
0.0.0.0 web-api-tih.acronis.com
127.0.0.1 lmlicenses.wip4.adobe.com
127.0.0.1 lm.licenses.adobe.com
127.0.0.1 na1r.services.adobe.com
127.0.0.1 hlrcv.stage.adobe.com
127.0.0.1 practivate.adobe.com
127.0.0.1 genuine.adobe.com
127.0.0.1 prod.adobegenuine.com

+>02:33:10 AutoIt3.exe ended.rc:0
-##<02:33:10 #RequireAdmin AutoIt3Wrapper run of script Finished.
+>02:33:11 AutoIt3Wrapper Finished.
>Exit code: 0    Time: 6.77
 

 

I would love for it to be quiet
And will not see that notepad opens at all
Thank you

Link to comment
Share on other sites

I have no explanation why the filewrite on line 12 doesn't work.
There seems to be some authorization problem.
Try not to name the script  "c :\11.au3", but "c :\users\pc\desktop\test.au3 
Please put the console log back into a spoiler.

Thanks

Edited by Exit

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

I think there is a simple explanation:
The file is write-protected.
Here is a script to enter the URL in the hosts file anyway:

; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"

ConsoleWrite(@CRLF & "Admin rights are " & (IsAdmin() ? "" : "NOT ") & "detected." & @CRLF & @CRLF)
ConsoleWrite($sHostsFile & " is " & (StringInStr(FileGetAttrib($sHostsFile), "R") ? "readonly." : "writable.") & @CRLF & @CRLF)

$sText = FileRead($sHostsFile)
ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)

If StringInStr($sText, $sURL) Then
    ConsoleWrite("URL already present" & @CRLF)
Else
    $iReadonly = StringInStr(FileGetAttrib($sHostsFile), "R")
    If $iReadonly Then FileSetAttrib($sHostsFile, "-R")
    ConsoleWrite($sHostsFile & " is " & (StringInStr(FileGetAttrib($sHostsFile), "R") ? "readonly." : "writable.") & @CRLF & @CRLF)
    $rc = FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & @TAB & $sURL & @CRLF)
    ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & " RC: " & $rc & @LF)
    If $iReadonly Then FileSetAttrib($sHostsFile, "+R")
    ConsoleWrite($sHostsFile & " is " & (StringInStr(FileGetAttrib($sHostsFile), "R") ? "readonly." : "writable.") & @CRLF & @CRLF)
    $sText = FileRead($sHostsFile)
    ConsoleWrite("Error: " & @error & " Extended: " & @extended & " Line: " & @ScriptLineNumber & @CRLF & @CRLF & $sText & @CRLF & @CRLF)
EndIf

ShellExecuteWait("Notepad.exe", $sHostsFile)
ShellExecute("Explorer.exe", StringTrimRight($sHostsFile, 6))

see a short version in next post

Edited by Exit

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...