Jump to content

(Solved) Blocking Address via Hosts file


PHAK
 Share

Recommended Posts

or the short version:

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

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

@Exit

1

I did run from a desktop

Because you asked for a consul I put in C

2

I did what you brought

You are a king 😃👍

The problem seems to have been

hosts

The file is write-protected.
3

I did not want him to open Notepad

So I deleted the lines below

; 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

 

Now there is another problem
Going down a row
And he put a profit

Example

127.0.0.1 prod.adobegenuine.com

127.0.0.1        www.facebook.com

There is something to do?

It does not bother
But if there is a solution I would be happy 😀

Link to comment
Share on other sites

13 hours ago, PHAK said:

Now there is another problem
Going down a row
And he put a profit

We have a language barrier here.
I can't understand the word profit.
Can you describe the problem in your language?
I will then translate it into German.

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

  • Developers
26 minutes ago, Exit said:

I can't understand the word profit.

advance...extra tabs...????

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

@Exit @GokAy

I got along

I had to remove

@CRLF  @TAB

It's revised

; disable "www.facebook.com"
#RequireAdmin
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"
$iReadonly = StringInStr(FileGetAttrib($sHostsFile), "R")
If $iReadonly Then FileSetAttrib($sHostsFile, "-R")
If Not StringInStr(FileRead($sHostsFile), $sURL) Then FileWrite($sHostsFile, @CRLF & "127.0.0.1" & @TAB & $sURL )
If $iReadonly Then FileSetAttrib($sHostsFile, "+R")

Thank you all

Link to comment
Share on other sites

Link to comment
Share on other sites

; enable "www.facebook.com"
#RequireAdmin
#include <Array.au3>
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"
$iReadonly = StringInStr(FileGetAttrib($sHostsFile), "R")
If $iReadonly Then FileSetAttrib($sHostsFile, "-R")
$aFile = FileReadToArray($sHostsFile)
For $i = @extended - 1 To 0 Step -1
    If StringInStr($aFile[$i], $surl) Then _ArrayDelete($aFile, $i)
Next
FileDelete($sHostsFile)
FileWrite($sHostsFile,_ArrayToString($aFile,@CRLF))
If $iReadonly Then FileSetAttrib($sHostsFile, "+R")

 

App: Au3toCmd              UDF: _SingleScript()                             

Link to comment
Share on other sites

2 hours ago, Exit said:
; enable "www.facebook.com"
#RequireAdmin
#include <Array.au3>
$sURL = "www.facebook.com"
$sHostsFile = @WindowsDir & "\system32\drivers\etc\hosts"
$iReadonly = StringInStr(FileGetAttrib($sHostsFile), "R")
If $iReadonly Then FileSetAttrib($sHostsFile, "-R")
$aFile = FileReadToArray($sHostsFile)
For $i = @extended - 1 To 0 Step -1
    If StringInStr($aFile[$i], $surl) Then _ArrayDelete($aFile, $i)
Next
FileDelete($sHostsFile)
FileWrite($sHostsFile,_ArrayToString($aFile,@CRLF))
If $iReadonly Then FileSetAttrib($sHostsFile, "+R")

 

Excellent
Amazing
This is what I wanted
Thank you
Champion

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