Guys: Watch the age of the thread -- This one has been stagnant for two years. Our Policy.
Closing
]]>#!/bin/bash
#
# 2012 Ontobelli for this script
# make hosts temporal directory
HOSTSDIR=~/.hostsupdate
mkdir -p "${HOSTSDIR}"
# make temporary directory
TMPDIR=/tmp/hostsupdate
mkdir -p "${TMPDIR}"
# set output file
OUTPUTFILE="${TMPDIR}/hosts"
# set temporal file
TMPFILE="${TMPDIR}/tmpfile"
if [ ! -f "${HOSTSDIR}/hosts.local" ]; then
echo "You need to create "${HOSTSDIR}"/hosts.local containing the hosts you wish to keep!"
exit 0
fi
# download the mvps.org hosts file.
wget -c -O "${HOSTSDIR}/hosts.mvps" "http://winhelp2002.mvps.org/hosts.txt"
# download hpHOSTS
wget -c -O "${HOSTSDIR}/hosts.hphosts" "http://support.it-mate.co.uk/downloads/HOSTS.txt"
# download hpHOSTS Partial
wget -c -O "${HOSTSDIR}/hosts.partial" "http://hosts-file.net/hphosts-partial.asp"
# download hpHOSTS ad/tracking servers
wget -c -O "${HOSTSDIR}/hosts.adservers" "http://hosts-file.net/ad_servers.asp"
# download the pgl.yoyo.org hosts Peter Lowe - AdServers
wget -c -O "${HOSTSDIR}/hosts.yoyo" "http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"
# download SysCtl Cameleon hosts
wget -c -O "${HOSTSDIR}/hosts.sysctl" "http://sysctl.org/cameleon/hosts"
# cat entries in a single file
cat "${HOSTSDIR}/hosts.mvps" > "${TMPFILE}0"
cat "${HOSTSDIR}/hosts.hphosts" >> "${TMPFILE}0"
cat "${HOSTSDIR}/hosts.partial" >> "${TMPFILE}0"
cat "${HOSTSDIR}/hosts.adservers" >> "${TMPFILE}0"
cat "${HOSTSDIR}/hosts.yoyo" >> "${TMPFILE}0"
cat "${HOSTSDIR}/hosts.sysctl" >> "${TMPFILE}0"
# tabs to space
sed -e 's/ / /g' "${TMPFILE}0" > "${TMPFILE}1"
# find relevant lines without comments
grep ^127.0.0.1 "${TMPFILE}1" > "${TMPFILE}2"
# remove duplicate spaces
cat "${TMPFILE}2" | tr -s [:space:] > "${TMPFILE}3"
# remove carriage returns
cat "${TMPFILE}3" | tr -d "\r" > "${TMPFILE}4"
# 0.0.0.0 is nicer than constantly knocking on localhosts' door.
sed -e 's/127.0.0.1 /0.0.0.0 /g' "${TMPFILE}4" > "${TMPFILE}5"
# remove inline comments
cut -d ' ' -f -2 "${TMPFILE}5" > "${TMPFILE}6"
# sort blocklist entries and remove duplicates
sort "${TMPFILE}6" | uniq > "${TMPFILE}7"
# remove unneeded blocked sites
grep -Ev ' dl.dropbox.com| host_you_want_to_whitelist' "${TMPFILE}7" > "${TMPFILE}9"
# write the user's hosts.local to head, then the blacklists
cat "${HOSTSDIR}"/hosts.local > "${OUTPUTFILE}"
cat "${TMPFILE}9" >> "${OUTPUTFILE}"
echo -e "# end of file" >> "${OUTPUTFILE}"
# move to /etc/hosts
mv "${OUTPUTFILE}" /etc/hosts
# delete temporary directory
rm -r -f "${TMPDIR}"
hosts.local
# /etc/hosts: static lookup table for host names
#
#<ip> <hostname.domain.org> <hostname>
127.0.0.1 localhost.localdomain localhost YOURHOSTSNAMEHERE
::1 localhost.localdomain localhost YOURHOSTSNAMEHERE
# YOUR PERSONAL list
# blocked list
Create an alias in your ~/.bashrc
alias hu='sudo /root/.hostsupdate/hosts_update'
Run
# hu <enter>
Script and cache must be located in /root/.hostsupdate or modify scrip accordingly
Cheers.
]]>Cool, have been looking into this myself but wasn't happy with the duplicate
127.0.0.1 localhost
that is always present in the downloaded file.
I've put this in at the end to remove the duplicate "127.0.0.1 localhost"
sed -i -e 's-^127.0.0.1 localhost-#-' /etc/hosts
Seems to work, is probably a more elegant way though!
Just edit the last line of my edit:
wget -O - "${ADBLOCK}" | sed /localhost/d >> /etc/hosts
127.0.0.1 localhost
that is always present in the downloaded file.
I've put this in at the end to remove the duplicate "127.0.0.1 localhost"
sed -i -e 's-^127.0.0.1 localhost-#-' /etc/hosts
Seems to work, is probably a more elegant way though!
]]>I made a few modifications to this code, added some basic error checking, and made it more efficient, hope you enjoy.
#!/bin/bash
ADBLOCK='http://www.mvps.org/winhelp2002/hosts.txt';
HOSTSHEAD=/etc/hosts_head;
if [ $(whoami) != "root" ]; then
echo "Please run as root." >&2
exit 1
fi
if [ ! -f "${HOSTSHEAD}" ]; then
echo "Header file for hosts missing." >&2
exit 2
fi
if [ -z "$(curl -I "${ADBLOCK}" 2>&1 | grep '200 OK' )" ]; then
echo "Error, return code for Adblock lists is not 200." >&2
echo "This typically indicates an error, rather than anything else." >&2
exit 3
fi
cp "${HOSTSHEAD}" /etc/hosts
wget -O - "${ADBLOCK}" | sed /localhost/d >> /etc/hosts
EDITED to clean up a little.
]]># lil' script to update /etc/hosts adblock-filter
#
#hosts adblock filter taken from this site...
wget --directory-prefix=/tmp http://www.mvps.org/winhelp2002/hosts.txt
#Backup /etc/hosts to /tmp
cp /etc/hosts /tmp
#standard static hosts file
echo '# /etc/hosts: static lookup table for host names' > /etc/hosts
echo '#' >> /etc/hosts
echo '#<ip-address> <hostname.domain.org> <hostname>' >> /etc/hosts
echo '127.0.0.1 localhost.localdomain localhost' >> /etc/hosts
#add custom statc host configuration here
echo ' ' >> /etc/hosts
echo '###Ad-Blocking###' >> /etc/hosts
cat /tmp/hosts.txt >> /etc/hosts
echo '# End of file' >> /etc/hosts
rm /tmp/hosts.txt
enjoy!
]]>