Skip to content

Instantly share code, notes, and snippets.

@Dani3lSun
Last active April 20, 2023 11:49
Show Gist options
  • Save Dani3lSun/83ebfa77fbb21a6873ef to your computer and use it in GitHub Desktop.
Save Dani3lSun/83ebfa77fbb21a6873ef to your computer and use it in GitHub Desktop.

Revisions

  1. Daniel Hochleitner revised this gist Nov 24, 2016. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -89,6 +89,7 @@ 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
  2. Daniel Hochleitner revised this gist Nov 24, 2016. 1 changed file with 13 additions and 4 deletions.
    17 changes: 13 additions & 4 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -64,6 +64,13 @@ 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'
    @@ -74,13 +81,15 @@ 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 's/127.0.0.1 localhost//g' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1 localhost//g' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 piwik.org//g' hosts_new_temp.txt
    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 's/127.0.0.1/0.0.0.0/' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 spclient.wg.spotify.com//g' hosts_new_temp.txt

    # build new hosts file
    cat hosts_org.txt > hosts_new.txt
  3. Daniel Hochleitner revised this gist Nov 4, 2016. 1 changed file with 6 additions and 6 deletions.
    12 changes: 6 additions & 6 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -30,7 +30,7 @@ mkdir www_hosts
    cd www_hosts

    # Host 1
    curl -O http://adaway.org/hosts.txt
    curl -O https://adaway.org/hosts.txt
    mv hosts.txt hosts1.txt
    if [ -f hosts1.txt ]; then
    echo '--> adaway.org saved';
    @@ -76,11 +76,11 @@ cat www_hosts/hosts4.txt >> hosts_new_temp.txt
    cat www_hosts/hosts5.txt >> hosts_new_temp.txt

    # Replace special entries
    sed -i.bak 's/127.0.0.1 localhost//' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1 localhost//' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 piwik.org//' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1 localhost//g' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1 localhost//g' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 piwik.org//g' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1/0.0.0.0/' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 spclient.wg.spotify.com//' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 spclient.wg.spotify.com//g' hosts_new_temp.txt

    # build new hosts file
    cat hosts_org.txt > hosts_new.txt
    @@ -90,7 +90,7 @@ 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
    rm -f hosts_new_temp.txt*

    # write new hosts file into system
    echo '!! Step 4: write new hosts file into system'
  4. Daniel Hochleitner revised this gist Nov 4, 2016. 1 changed file with 12 additions and 17 deletions.
    29 changes: 12 additions & 17 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -69,33 +69,28 @@ fi
    echo '!! Step 3: merge files into new host file'
    cd ..

    cat www_hosts/hosts1.txt >> hosts_new1.txt
    cat www_hosts/hosts2.txt >> hosts_new1.txt
    cat www_hosts/hosts3.txt >> hosts_new1.txt
    cat www_hosts/hosts4.txt >> hosts_new1.txt
    cat www_hosts/hosts5.txt >> hosts_new1.txt
    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

    # Replace special entries
    cat hosts_new1.txt | sed 's/127.0.0.1 localhost//' > hosts_new2.txt
    cat hosts_new2.txt | sed 's/127.0.0.1 localhost//' > hosts_new3.txt
    cat hosts_new3.txt | sed 's/0.0.0.0 piwik.org//' > hosts_new4.txt
    cat hosts_new4.txt | sed 's/127.0.0.1/0.0.0.0/' > hosts_new5.txt
    cat hosts_new5.txt | sed 's/0.0.0.0 spclient.wg.spotify.com//' > hosts_new6.txt
    sed -i.bak 's/127.0.0.1 localhost//' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1 localhost//' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 piwik.org//' hosts_new_temp.txt
    sed -i.bak 's/127.0.0.1/0.0.0.0/' hosts_new_temp.txt
    sed -i.bak 's/0.0.0.0 spclient.wg.spotify.com//' 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_new6.txt >> hosts_new.txt
    cat hosts_new_temp.txt >> hosts_new.txt

    rm -f hosts_new1.txt
    rm -f hosts_new2.txt
    rm -f hosts_new3.txt
    rm -f hosts_new4.txt
    rm -f hosts_new5.txt
    rm -f hosts_new6.txt
    rm -f hosts_new_temp.txt

    # write new hosts file into system
    echo '!! Step 4: write new hosts file into system'
  5. Daniel Hochleitner revised this gist Jun 24, 2016. 1 changed file with 3 additions and 1 deletion.
    4 changes: 3 additions & 1 deletion adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -80,20 +80,22 @@ cat hosts_new1.txt | sed 's/127.0.0.1 localhost//' > hosts_new2.txt
    cat hosts_new2.txt | sed 's/127.0.0.1 localhost//' > hosts_new3.txt
    cat hosts_new3.txt | sed 's/0.0.0.0 piwik.org//' > hosts_new4.txt
    cat hosts_new4.txt | sed 's/127.0.0.1/0.0.0.0/' > hosts_new5.txt
    cat hosts_new5.txt | sed 's/0.0.0.0 spclient.wg.spotify.com//' > hosts_new6.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_new5.txt >> hosts_new.txt
    cat hosts_new6.txt >> hosts_new.txt

    rm -f hosts_new1.txt
    rm -f hosts_new2.txt
    rm -f hosts_new3.txt
    rm -f hosts_new4.txt
    rm -f hosts_new5.txt
    rm -f hosts_new6.txt

    # write new hosts file into system
    echo '!! Step 4: write new hosts file into system'
  6. Dani3lSun revised this gist Feb 22, 2016. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -57,6 +57,13 @@ 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

    # merge files into new host file
    #
    echo '!! Step 3: merge files into new host file'
    @@ -66,6 +73,7 @@ cat www_hosts/hosts1.txt >> hosts_new1.txt
    cat www_hosts/hosts2.txt >> hosts_new1.txt
    cat www_hosts/hosts3.txt >> hosts_new1.txt
    cat www_hosts/hosts4.txt >> hosts_new1.txt
    cat www_hosts/hosts5.txt >> hosts_new1.txt

    # Replace special entries
    cat hosts_new1.txt | sed 's/127.0.0.1 localhost//' > hosts_new2.txt
  7. Dani3lSun revised this gist Sep 10, 2015. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -75,6 +75,10 @@ cat hosts_new4.txt | sed 's/127.0.0.1/0.0.0.0/' > hosts_new5.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_new5.txt >> hosts_new.txt

    rm -f hosts_new1.txt
  8. Dani3lSun created this gist Sep 10, 2015.
    95 changes: 95 additions & 0 deletions adblock_hosts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,95 @@
    #!/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 http://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

    # merge files into new host file
    #
    echo '!! Step 3: merge files into new host file'
    cd ..

    cat www_hosts/hosts1.txt >> hosts_new1.txt
    cat www_hosts/hosts2.txt >> hosts_new1.txt
    cat www_hosts/hosts3.txt >> hosts_new1.txt
    cat www_hosts/hosts4.txt >> hosts_new1.txt

    # Replace special entries
    cat hosts_new1.txt | sed 's/127.0.0.1 localhost//' > hosts_new2.txt
    cat hosts_new2.txt | sed 's/127.0.0.1 localhost//' > hosts_new3.txt
    cat hosts_new3.txt | sed 's/0.0.0.0 piwik.org//' > hosts_new4.txt
    cat hosts_new4.txt | sed 's/127.0.0.1/0.0.0.0/' > hosts_new5.txt

    # build new hosts file
    cat hosts_org.txt > hosts_new.txt
    cat hosts_new5.txt >> hosts_new.txt

    rm -f hosts_new1.txt
    rm -f hosts_new2.txt
    rm -f hosts_new3.txt
    rm -f hosts_new4.txt
    rm -f hosts_new5.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 !!'