Username Always stay logged in
Password:

 

Pages: [1]
  Print  
Author Topic: hosts file  (Read 2645 times)
martinc
Global Moderator
Pro Designer
*****

Karma: +1/-0
Offline Offline

Posts: 148



View Profile
« on: January 17, 2012, 06:14:02 PM »

By adding urls of know ad severs, malware, etc to your hosts file, it loops them back to 127.0.0.1 so it never loads making your browsing quicker and safer

hosts for download
Blocking Unwanted Parasites with a Hosts File is earmarked for non commercial use
how to make the internet not suck (as much) doesn't was either way

Locations:
Mac
     /private/etc/hosts
Lin
     /etc/hosts
Win
     /windows/system32/drivers/etc/hosts


Code:
# download directly to hosts file
wget -O /etc/hosts http://someonewhocares.org/hosts/hosts

Code:
# or something along the lines of lil' script to update /etc/hosts adblock-filter, https://bbs.archlinux.org/viewtopic.php?id=80204

# lil' script to update /etc/hosts adblock-filter

#download hosts file
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 static host configuration here
echo ' ' >> /etc/hosts
echo '###Ad-Blocking###' >> /etc/hosts

# drop your original entries back in
cat /tmp/hosts.txt >> /etc/hosts
echo '# End of file' >> /etc/hosts

#clean up
rm /tmp/hosts.txt

with the nicest being from the same site
Code:
#!/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}"

for mac : replace wget -c -O with curl -o

Code:
#!/bin/bash
#
# Inspired in graysky hostsupdate script, gaenserich hostsblock script and HostMan
# [c] Ontobelli  Version: 2012-08-06 07:26 UTC
#

echo " Started hosts updater `date +'%x %T'`"
# CHECK FOR NEEDED AND OPTIONAL UTILITIES AND PERMISSIONS
if [ `whoami` != "root" ]; then
 echo "Insufficient permissions. Run as root."
 exit 0
fi

HOSTSDIR="/etc/hosts.d/"
# I decided to leave a tmp folder cache because sometimes servers are not up and
# hosts file is incomplet otherwise and could be a risk.
TMPDIR="${HOSTSDIR}tmp/"
OUTPUTFILE="/etc/hosts"

if [ ! -d "${TMPDIR}" ]; then
 mkdir -p "${TMPDIR}"
fi

if [ ! -f "${HOSTSDIR}hosts.local" ]; then
 echo "You need to create "${HOSTSDIR}hosts.local" containing the hosts you wish to keep!"
 echo "fe00::0 ip6-localnet" > "${HOSTSDIR}hosts.local"
 echo "ff00::0 ip6-mcastprefix" >> "${HOSTSDIR}hosts.local"
 echo "ff02::1 ip6-allnodes" >> "${HOSTSDIR}hosts.local"
 echo "ff02::2 ip6-allrouters" >> "${HOSTSDIR}hosts.local"
 echo "ff02::3 ip6-allhosts" >> "${HOSTSDIR}hosts.local"
fi

if [ ! -f "${HOSTSDIR}hosts.whitelist" ]; then
 echo "You need to create "${HOSTSDIR}hosts.whitelist" containing the hosts you want to discard from the lists!"
 echo -e "#" > "${HOSTSDIR}hosts.whitelist"
fi

echo Downloading hosts.hphosts...
curl --compressed -s -o "${TMPDIR}hosts.hphosts" "http://hosts-file.net/download/hosts.txt"
echo Downloading hosts.adservers...
curl --compressed -s -o "${TMPDIR}hosts.adservers" "http://hosts-file.net/ad_servers.asp"
echo Downloading hosts.partial...
curl --compressed -s -o "${TMPDIR}hosts.partial" "http://hosts-file.net/hphosts-partial.asp"
echo Downloading hosts.mvps...
curl --compressed -s -o "${TMPDIR}hosts.mvps" "http://winhelp2002.mvps.org/hosts.txt"
echo Downloading hosts.minenu...
curl --compressed -s -o "${TMPDIR}hosts.minenu" "http://hostsfile.mine.nu/Hosts"
echo Downloading hosts.malware...
curl --compressed -s -o "${TMPDIR}hosts.malware" "http://www.malwaredomainlist.com/hostslist/hosts.txt"
echo Downloading hosts.sysctl...
curl --compressed -s -o "${TMPDIR}hosts.sysctl" "http://sysctl.org/cameleon/hosts"
echo Downloading hosts.sowc...
curl --compressed -s -o "${TMPDIR}hosts.sowc" "http://someonewhocares.org/hosts/hosts"
echo Downloading hosts.ismeh...
curl --compressed -s -o "${TMPDIR}hosts.ismeh" "http://www.ismeh.com/HOSTS"
echo Downloading hosts.securemecca...
curl --compressed -s -o "${TMPDIR}hosts.securemecca" "http://www.securemecca.com/Downloads/hosts.txt"
echo Downloading hosts.yoyo...
curl --compressed -s -o "${TMPDIR}hosts.yoyo" "http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext"

# hosts header
echo "127.0.0.1 ${HOSTNAME} localhost.localdomain localhost loopback" > "${OUTPUTFILE}"
echo "::1 ${HOSTNAME} localhost.localdomain localhost loopback ip6-localhost ip6-loopback" >> "${OUTPUTFILE}"
cat "${HOSTSDIR}hosts.local"  >> "${OUTPUTFILE}"

cat "${TMPDIR}hosts.hphosts" \
    "${TMPDIR}hosts.adservers" \
    "${TMPDIR}hosts.partial" \
    "${TMPDIR}hosts.mvps" \
    "${TMPDIR}hosts.minenu" \
    "${TMPDIR}hosts.malware" \
    "${TMPDIR}hosts.sysctl" \
    "${TMPDIR}hosts.sowc" \
    "${TMPDIR}hosts.ismeh" \
    "${TMPDIR}hosts.securemecca" \
    "${TMPDIR}hosts.yoyo" | \
sed -e 's/ / /g' | \
grep ^127.0.0.1 | \
tr -d "\r" | \
tr -s [:space:] | \
cut -d " " -f -2 | \
sort -u | \
grep -vf "${HOSTSDIR}hosts.whitelist" | \
#sed -e 's/127.0.0.1 /127.0.0.1 /g' >> "${OUTPUTFILE}"  # in case you use a http server
sed -e 's/127.0.0.1 /0.0.0.0 /g' >> "${OUTPUTFILE}"  # in case you don't use a http server

#rm -f -r ${TMPDIR}

/etc/rc.d/dnsmasq restart # in case you use dnsmasq
echo "Done! `date +'%x %T'`"

Modified
   echo -e was echoing the -e
   grep -e '127.0.0.1 ' | \ wasn't working, switched back to grep ^127.0.0.1 | \
   use 0.0.0.0
  timestamp

orig

Changelog: simplified lists call
Code:
#!/bin/bash

echo " Started hosts updater `date +'%x %T'`"

if [ `whoami` != "root" ]; then
 echo "Insufficient permissions. Run as root."
 exit 0
fi

SW_ROOT="/etc/"
HOSTSDIR="${SW_ROOT}hosts.d/"
TMPDIR="${HOSTSDIR}tmp/"
OUTPUTFILE="${SW_ROOT}hosts"

if [ ! -d "${TMPDIR}" ]; then
 mkdir -p "${TMPDIR}"
fi

if [ ! -d "${HOSTSDIR}" ]; then
 mkdir -p "${HOSTSDIR}"
fi

if [ ! -f "${HOSTSDIR}hosts.local" ]; then
 echo "You need to create "${HOSTSDIR}hosts.local" containing the hosts you wish to keep!"
 echo "127.0.0.1 ${HOSTNAME} localhost.localdomain localhost loopback" > "${HOSTSDIR}hosts.local"
 echo "::1 ${HOSTNAME} localhost.localdomain localhost loopback ip6-localhost ip6-loopback" >> "${HOSTSDIR}hosts.local"
 echo "fe00::0 ip6-localnet" >> "${HOSTSDIR}hosts.local"
 echo "ff00::0 ip6-mcastprefix" >> "${HOSTSDIR}hosts.local"
 echo "ff02::1 ip6-allnodes" >> "${HOSTSDIR}hosts.local"
 echo "ff02::2 ip6-allrouters" >> "${HOSTSDIR}hosts.local"
 echo "ff02::3 ip6-allhosts" >> "${HOSTSDIR}hosts.local"
fi

if [ ! -f "${HOSTSDIR}hosts.whitelist" ]; then
 echo "You need to create "${HOSTSDIR}hosts.whitelist" containing the hosts you want to discard from the lists!"
 echo "1.1.1.1    test.com" > "${HOSTSDIR}hosts.whitelist"
fi

url=('http://winhelp2002.mvps.org/hosts.txt'
 'http://hosts-file.net/download/hosts.txt'
 'http://hosts-file.net/ad_servers.asp'
 'http://hosts-file.net/hphosts-partial.asp'
 'http://hostsfile.mine.nu/Hosts'
 'http://www.malwaredomainlist.com/hostslist/hosts.txt'
 'http://sysctl.org/cameleon/hosts'
 'http://someonewhocares.org/hosts/hosts'
 'http://www.ismeh.com/HOSTS'
 'http://www.securemecca.com/Downloads/hosts.txt'
 'http://pgl.yoyo.org/as/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext')

name=('mvps'
 'hphosts'
 'adservers'
 'partial'
 'minenu'
 'malware'
 'sysctl'
 'sowc'
 'ismeh'
 'securemecca'
 'yoyo')

ok='echo -e " ✔ ok"'
fail='echo -e " ✘ fail"'

for (( i=0; i<${#url[@]}; i++ )); do
 echo -en "[${name[$i]}]\n\tdownloading» "
    curl -L "${url[$i]}" -o "${TMPDIR}hosts.${name[$i]}" &>/dev/null  &&
 eval $ok || eval $fail
done

echo "Combining and sorting . . ."
cat "${HOSTSDIR}hosts.local" > "${OUTPUTFILE}"
cat "${TMPDIR}hosts."* | \
sed -e 's/ / /g' | \
grep ^127.0.0.1 | \
tr -d "\r" | \
tr -s [:space:] | \
cut -d " " -f -2 | \
sort -u | \
grep -vf "${HOSTSDIR}hosts.whitelist" | \
sed -e 's/127.0.0.1 /0.0.0.0 /g' >> "${OUTPUTFILE}" &&
eval $ok || eval $fail

#rm -f -r ${TMPDIR}

/etc/rc.d/dnsmasq restart # in case you use dnsmasq

echo "Done! `date +'%x %T'`"

Logged

I have as much authority as the Pope, I just don't have as many people who believe it. --George Carlin
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.21 | SMF © 2015, Simple Machines
Greenday Theme Designed By [S.W.T]