VSIDO Community

VSIDO Support => Scripts and How To's => Scripts => Topic started by: hakerdefo on March 25, 2014, 10:55:20 AM

Title: Adblock Script
Post by: hakerdefo on March 25, 2014, 10:55:20 AM
As reported here,
http://vsido.org/index.php?topic=752.msg8424#msg8424 (http://vsido.org/index.php?topic=752.msg8424#msg8424)

and here,
http://vsido.org/index.php?topic=752.msg8447#msg8447 (http://vsido.org/index.php?topic=752.msg8447#msg8447)

It seems AdBlock is going astray. Here is a simpler and system wide alternative to AdBlock. Just save this bash script somewhere in your path, Give it 'Execute' permission, Run it and follow simple instruction given by the script. That's it. You will have a system wide, Cross browser adblocker deployed. And yes don't forget to run the script once a week to keep the database updated.

Code: [Select]

# If this is our first run, save a copy of the system's original hosts file and set to read-only for safety
if [ ! -f ~/.hosts-system ]
 echo "Saving copy of system's original hosts file..."
 cp /etc/hosts ~/.hosts-system
 chmod 444 ~/.hosts-system

# Perform work in temporary files

# Obtain various hosts files and merge into one
echo "Downloading ad-blocking hosts files..."
wget -nv -O - http://winhelp2002.mvps.org/hosts.txt >> "$temphosts1"
wget -nv -O - http://hosts-file.net/ad_servers.asp >> "$temphosts1"
wget -nv -O - http://someonewhocares.org/hosts/hosts >> "$temphosts1"
wget -nv -O - "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" >> "$temphosts1"

# Do some work on the file:
# 1. Remove MS-DOS carriage returns
# 2. Delete all lines that don't begin with
# 3. Delete any lines containing the word localhost because we'll obtain that from the original hosts file
# 4. Replace with because then we don't have to wait for the resolver to fail
# 5. Scrunch extraneous spaces separating address from name into a single tab
# 6. Delete any comments on lines
# 7. Clean up leftover trailing blanks
# Pass all this through sort with the unique flag to remove duplicates and save the result
echo "Parsing, cleaning, de-duplicating, sorting..."
sed -e 's/\r//' -e '/^!d' -e '/localhost/d' -e 's/' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < "$temphosts1" | sort -u > "$temphosts2"

# Combine system hosts with adblocks
echo Merging with original system hosts...
echo -e "\n# Ad blocking hosts generated" "$date" | cat ~/.hosts-system - "$temphosts2" > ~/.hosts-block

# Clean up temp files and remind user to copy new file
echo "Cleaning up..."
rm "$temphosts1" "$temphosts2"
echo "Done."
echo "Copy ad-blocking hosts file with this command:"
echo "sudo cp ~/.hosts-block /etc/hosts"
echo "You can always restore your original hosts file with this command:"
echo "sudo cp ~/.hosts-system /etc/hosts"
echo "so don't delete that file! (It's saved read-only for your protection.)"

Title: Re: Adblock Script
Post by: VastOne on March 25, 2014, 01:35:00 PM
Wow, very nice How To hakerdefo

Title: Re: Adblock Script
Post by: hakerdefo on March 26, 2014, 05:10:04 PM
Thanks VastOne!
Title: Re: Adblock Script
Post by: Digit on February 20, 2015, 05:44:51 AM
awesome.  idk how i missed this first time around.  (thnx orbea for linking).

better than just someonewhocares' hostfile.   :)