Skip to content

Instantly share code, notes, and snippets.

@b0gdanw
Forked from Dani3lSun/adblock_hosts.sh
Last active July 23, 2021 18:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b0gdanw/2716bc05be8eef49424b105d39370105 to your computer and use it in GitHub Desktop.
Save b0gdanw/2716bc05be8eef49424b105d39370105 to your computer and use it in GitHub Desktop.
Mac OS X adblock shell script using system hosts file (Block ads system wide)
#!/bin/sh
#
# Mac OSX Adblocker Script for IPv4
# Description: Blocks ads using system hosts file /private/etc/hosts, Ad-Domains would be redirected to 0.0.0.0
# Author: Daniel Hochleitner
# Created: 10.09.2015
# Use: sudo ./adblock_hosts.sh
# Get original hosts file from /private/etc/hosts
#
echo '!! Step 1: Get original hosts file'
# into current directory
if [ -f hosts_org.txt ]; then
echo '--> original hosts file already saved in current directory';
else
cp /private/etc/hosts hosts_org.txt
fi
# save into /private/etc/hosts_org
if [ -f /private/etc/hosts_org ]; then
echo '--> original hosts file already saved in /private/etc/hosts_org';
else
sudo cp -p /private/etc/hosts /private/etc/hosts_org
echo '--> saved hosts file into /private/etc/hosts_org'
fi
echo '!! Step 2: Get Adblock hosts files from WWW'
rm -fr www_hosts
mkdir www_hosts
cd www_hosts
# Host 1
curl -O https://raw.githubusercontent.com/AdAway/adaway.github.io/master/hosts.txt
mv hosts.txt hosts1.txt
if [ -f hosts1.txt ]; then
echo '--> adaway.org saved';
fi
# Host 2
curl -O https://hosts-file.net/ad_servers.txt
mv ad_servers.txt hosts2.txt
if [ -f hosts2.txt ]; then
echo '--> hosts-file.net saved';
fi
# Host 3
curl -O http://winhelp2002.mvps.org/hosts.txt
mv hosts.txt hosts3.txt
if [ -f hosts3.txt ]; then
echo '--> winhelp2002.mvps.org saved';
fi
# Host 4
curl -O "https://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
mv serverlist.php\?hostformat\=hosts\&showintro\=0\&mimetype\=plaintext hosts4.txt
if [ -f hosts4.txt ]; then
echo '--> pgl.yoyo.org saved';
fi
# Host 5
curl -O https://www.malwaredomainlist.com/hostslist/hosts.txt
mv hosts.txt hosts5.txt
if [ -f hosts5.txt ]; then
echo '--> malwaredomainlist saved';
fi
# Host 6
curl -O https://someonewhocares.org/hosts/zero/hosts
mv hosts hosts6.txt
if [ -f hosts6.txt ]; then
echo '--> someonewhocares.org saved';
fi
# Host 7
curl -O https://raw.githubusercontent.com/hoshsadiq/adblock-nocoin-list/master/hosts.txt
mv hosts.txt hosts7.txt
if [ -f hosts7.txt ]; then
echo '--> nocoin-list saved';
fi
# merge files into new host file
#
echo '!! Step 3: merge files into new host file'
cd ..
cat www_hosts/hosts1.txt >> hosts_new_temp.txt
cat www_hosts/hosts2.txt >> hosts_new_temp.txt
cat www_hosts/hosts3.txt >> hosts_new_temp.txt
cat www_hosts/hosts4.txt >> hosts_new_temp.txt
cat www_hosts/hosts5.txt >> hosts_new_temp.txt
cat www_hosts/hosts6.txt >> hosts_new_temp.txt
cat www_hosts/hosts7.txt >> hosts_new_temp.txt
# Replace special entries
sed -i.bak '/localhost/d' hosts_new_temp.txt
sed -i.bak '/broadcasthost/d' hosts_new_temp.txt
sed -i.bak '/local/d' hosts_new_temp.txt
sed -i.bak '/piwik.org/d' hosts_new_temp.txt
sed -i.bak '/spclient.wg.spotify.com/d' hosts_new_temp.txt
sed -i.bak '/securemetrics.apple.com/d' hosts_new_temp.txt
sed -i.bak 's/127.0.0.1/0.0.0.0/' hosts_new_temp.txt
# build new hosts file
cat hosts_org.txt > hosts_new.txt
# special 0.0.0.0 entry
echo "# Special Entries" >> hosts_new.txt
echo "0.0.0.0 0.0.0.0 # fix for traceroute and netstat" >> hosts_new.txt
# adblock hosts entries
cat hosts_new_temp.txt >> hosts_new.txt
# remove temporary files
rm -f hosts_new_temp.txt*
rm -fr www_hosts
# write new hosts file into system
echo '!! Step 4: write new hosts file into system'
echo '--> writing file...'
sudo cp hosts_new.txt /private/etc/hosts
chmod 644 /private/etc/hosts
echo '--> refresh DNS cache...'
dscacheutil -flushcache
sudo killall -HUP mDNSResponder
echo '!! Finished !!'
@tommymachine
Copy link

tommymachine commented Mar 10, 2021

Youtube Adservers?

... line 81 ...

# Youtube Ad Servers
curl -O https://raw.githubusercontent.com/Ewpratten/youtube_ad_blocklist/master/blocklist.txt
mv blocklist.txt hostsYouTube.txt
if [ -f hosts7.txt ]; then
  echo '--> youtube-list saved';
fi

... line 94 ...

cat www_hosts/hostsYouTube.txt >> hosts_new_temp.txt

but also, why not keep all list files in their own list, then loop through them?

https://www.cyberciti.biz/faq/bash-for-loop/

anyways thanks!

@b0gdanw
Copy link
Author

b0gdanw commented Mar 10, 2021

The list does not seem to work, empty frames are displayed and ads at video start. It might not be a good idea to block googlevideo.com
I'm currently using https://github.com/b0gdanw/AdAwayForSafari

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment