Skip to content

Instantly share code, notes, and snippets.

@louima
Last active August 30, 2017 23:27
Show Gist options
  • Save louima/878171fc67d797cfcef4 to your computer and use it in GitHub Desktop.
Save louima/878171fc67d797cfcef4 to your computer and use it in GitHub Desktop.
#!/bin/bash
files=(\
'https://adaway.org/hosts.txt'\
'http://winhelp2002.mvps.org/hosts.txt'\
'http://hosts-file.net/.\ad_servers.txt'\
'http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext'\
'http://someonewhocares.org/hosts/hosts'\
)
noip='127.0.0.1'
tmphosts=$(mktemp)
tmpraw=$(mktemp)
hostsfile=/etc/hosts
for item in ${files[*]}
do
echo "Downloading $item ..."
curl $item >> $tmpraw || { echo -e "\nError downloading $item"; exit 1; }
done
echo -e "127.0.0.1\tlocalhost $(hostname)\n" > $tmphosts
# \x0d = special ^M carriage return char on windows files
cat $tmpraw | sed 's/'$(echo "\x0d")'$//' |\
grep '^\(127.0.0.1\|0.0.0.0\)' |\
grep -v '\(\t\|\ \)*localhost$' |\
sed 's/\(\t\|\ \)*#.*$//' |\
sed "s/\(127.0.0.1\|0.0.0.0\)\(\t\|\ \)*/$noip\t/" |\
# blocking s.ytimg.com makes youtube unusable:
grep -Pv "$noip\ts.ytimg.com$" |\
# amazon images:
grep -Pv "$noip\tecx.images-amazon.com$" |\
sort | uniq >> $tmphosts
su -c "cp $tmphosts $hostsfile" && rm $tmpraw $tmphosts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment