Last active
April 20, 2023 11:49
-
-
Save Dani3lSun/83ebfa77fbb21a6873ef to your computer and use it in GitHub Desktop.
Mac OS X adblock shell script using system hosts file (Block ads system wide)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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://adaway.org/hosts.txt | |
mv hosts.txt hosts1.txt | |
if [ -f hosts1.txt ]; then | |
echo '--> adaway.org saved'; | |
fi | |
# Host 2 | |
curl -O http://hosts-file.net/.%5Cad_servers.txt | |
mv .%5Cad_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 "http://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 http://www.malwaredomainlist.com/hostslist/hosts.txt | |
mv hosts.txt hosts5.txt | |
if [ -f hosts5.txt ]; then | |
echo '--> malwaredomainlist saved'; | |
fi | |
# Host 6 | |
curl -O http://someonewhocares.org/hosts/zero/hosts | |
mv hosts hosts6.txt | |
if [ -f hosts6.txt ]; then | |
echo '--> someonewhocares.org 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 | |
# 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 | |
rm -f hosts_new_temp.txt* | |
# 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 !!' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
thanks for the correction...This script is pretty old, so the main reason why we see html/http output there is because the host files moved the server location, so if the URLs get updated it should work again...
Btw nowadays I use this:
https://github.com/StevenBlack/hosts
It's much more feature rich and can be nicely configured...