Jump to content

Blocking www.Facebook.com via Hosts file


 Share

Recommended Posts

Hey guys unfortunetly I am not allowed to use Perl here at my current job :) but in some ways am glad because it helped me find AUTOIT. ;)

Is there an easy way to add

127.0.0.1 www.facebook.com

to my C:\WINDOWS\system32\drivers\etc\Hosts file?

If its needed/helps it would be on line 25 in the file.

I would execute the code via a logon script so that the multiple abusers would no longer have access to it until I can get a better firewall to handle the job.

Any help is appreciated. Point me in the right direction if some similar code is out there, I have looked through the forums for a solution for some time now.

Link to comment
Share on other sites

Hey guys unfortunetly I am not allowed to use Perl here at my current job <_< but in some ways am glad because it helped me find AUTOIT. :P

Is there an easy way to add

127.0.0.1 www.facebook.com

to my C:\WINDOWS\system32\drivers\etc\Hosts file?

If its needed/helps it would be on line 25 in the file.

I would execute the code via a logon script so that the multiple abusers would no longer have access to it until I can get a better firewall to handle the job.

Any help is appreciated. Point me in the right direction if some similar code is out there, I have looked through the forums for a solution for some time now.

FileWriteLine()

...assuming you have permissions to the file.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

FileWriteLine()

...assuming you have permissions to the file.

<_<

Yes I have permissions, but here is the thing, It will have to be sent to %SystemRoot%\system32\drivers\etc because different operating systems are involved. Also the hosts file does not have a file extension I would have to run it with an "open with" function.

Maybe I should just create a hosts file and copy it to a server and just have the script copy the file and not worry about it writing to a specific line in the file.

What do you think would be the better route?

Link to comment
Share on other sites

Open the file with notepad

Make the entry like this:

# Copyright © 1993-1999 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

127.0.0.1 localhost

127.0.0.1 www.facebook.com

Do "file > save". Do not do "file > save as" for it will make it host.txt and that won't work
Link to comment
Share on other sites

Yes I have permissions, but here is the thing, It will have to be sent to %SystemRoot%\system32\drivers\etc because different operating systems are involved. Also the hosts file does not have a file extension I would have to run it with an "open with" function.

Maybe I should just create a hosts file and copy it to a server and just have the script copy the file and not worry about it writing to a specific line in the file.

What do you think would be the better route?

AutoIt has macros for things like @WinDir, @SystemDir, and the ability to read environment variables with EnvGet(), and the ability to expand environment variables in line with Opt("ExpandEnvStrings", 1). So that's not a problem.

What makes you think using a file without an extension is an issue?

Have you actually coded anything in AutoIt yet?

<_<

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

$hostPath = ""
Switch @OSType
    Case "WIN32_NT"
        $hostPath = @SystemDir & "\drivers\etc"
    Case "WIN32_WINDOWS"
        $hostPath = @WindowsDir
EndSwitch
FileWriteLine($hostPath & "\hosts", "127.0.0.1 www.facebook.com")

Edit: changed $hostPath's

Edit again: FileWriteLine is appending the line to the end of the previous line so you may want to do @LF & "127.0.0.1 www.facebook.com"

Edited by weaponx
Link to comment
Share on other sites

AutoIt has macros for things like @WinDir, @SystemDir, and the ability to read environment variables with EnvGet(), and the ability to expand environment variables in line with Opt("ExpandEnvStrings", 1). So that's not a problem.

What makes you think using a file without an extension is an issue?

Have you actually coded anything in AutoIt yet?

<_<

I am completely new to AutoIt, I usually use Perl for scripts and C# for programming, but where I work the Sr. Admin does not want Perl used and he prefers AutoIt or smaller sized scripting languages etc. And you can only do so much with windows batch files. So I am just starting to learn AutoIt.

Link to comment
Share on other sites

$hostPath = ""
Switch @OSType
    Case "WIN32_NT"
        $hostPath = @SystemDir & "\drivers\etc"
    Case "WIN32_WINDOWS"
        $hostPath = @WindowsDir
EndSwitch
FileWriteLine($hostPath & "\hosts", "127.0.0.1 www.facebook.com")

Edit: changed $hostPath's

Edit again: FileWriteLine is appending the line to the end of the previous line so you may want to do @LF & "127.0.0.1 www.facebook.com"

Thanks weaponx, it works great.

Link to comment
Share on other sites

Like I stated in the initial post it is just a temporary fix untill I get a new firewall so I can block it by url. But untill then this should shut them out and possibly discourage them from using Facebook on company time in the future. It is not by my decision to do this it is at a "higher up's" request as I personally try to find a balance and give the users as much freedom as possible.

Thanks for the posts everyone.

Link to comment
Share on other sites

  • Developers

Can't you add a dummy route of the Subnet used by this website to your DNS server ?

Jos

Edited by 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

  • Developers

I am not exactly sure what you mean Jos, care to expound?

I assume you have a Router between you LAn and the Internet ?

If so, most of these routers have the option to a a Fixed IP route.

So if that's a possibility you could add a route for the IP subnet that the host is located on (69.63.176.0 netmask 255.255.255.0 ) and point that to a none existing route.

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

I assume you have a Router between you LAn and the Internet ?

If so, most of these routers have the option to a a Fixed IP route.

So if that's a possibility you could add a route for the IP subnet that the host is located on (69.63.176.0 netmask 255.255.255.0 ) and point that to a none existing route.

Jos

Yes that sounds like a possible solution. I plan on blocking the url with the new firewall. The reason I was looking for a quick solution was to learn AutoIt better and to appease a direct order to immediately stop Faceboook useage. If worse came to worse I could have just blocked all internet access on the few abusers, but that seemed a little harsh.

Link to comment
Share on other sites

IIRC the hosts file requires at least 2 whitespaces between the 127.0.0.1 and the www.facebook.com for it to function

Make sure to test the hosts file before assuming you have solved the problem.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

am a little new to auto-it so forgive my newbiness but couldnt this be solved with

Run("notepad C:\Windows\sysetm32\drivers\etc\hosts")
Sleep(200)
Send(" [Down] 26")
Sleep(200)
Send("127.0.0.1  www.facebook.com")
Sleep(200)
Send("[Down]")
Send("127.0.0.1  facebook.com")
Sleep(200)
Send( ctrl + s )
Link to comment
Share on other sites

remember too that hosts is parsed from top to bottom -- the first match wins.

not likely that a previous entry will be in the file, bit if one were it would win over a simple FileWriteLine appending to the end of the file.

Reading the help file before you post... Not only will it make you look smarter, it will make you smarter.

Link to comment
Share on other sites

This should do exactly what you want:

#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

Hope you find it useful.

- The Kandie Man ;-)

Edited by The Kandie Man

"So man has sown the wind and reaped the world. Perhaps in the next few hours there will no remembrance of the past and no hope for the future that might have been." & _"All the works of man will be consumed in the great fire after which he was created." & _"And if there is a future for man, insensitive as he is, proud and defiant in his pursuit of power, let him resolve to live it lovingly, for he knows well how to do so." & _"Then he may say once more, 'Truly the light is sweet, and what a pleasant thing it is for the eyes to see the sun.'" - The Day the Earth Caught Fire

Link to comment
Share on other sites

@ The Kandie Man

That script looks like there is still only 1 space between the 127.0.0.1 and the address

I'm quite sure that the minumum is 2 and I know for sure that the MVPS hosts file uses 3 tabs to be sure of enough white space. I don't recall what the whitespace was for but I do now that it won't work if there is not enough.

Edit: I just checked and Microsoft claims it will work with 1 space for XP but even their examples use a minumum of 5 spaces.

Edited by GEOSoft

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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