Mint Serena 64 bit using the Mate desktop environment. Most of the following scripts will run on this machine, although some have only been tested with the Gnome desktop. Extra software requirements can be met by the Ubuntu and safe PPA repositories. Scripts may be re-written or added to from time to time, the date serves as the version number. Syntax highlighting on these pages is provided by GVim's "Convert to HTML" option.

Hosts file updater.


#!/bin/bash
# Filename: hosts-update ## file name changed, see "(bug #38022)"
# Version: 030711
# Author: robz
# The hosts file, simple text file that associates IP addresses with hostnames,
# http://en.wikipedia.org/wiki/Hosts_%28file%29 also some more reading and the
# zipped source file http://winhelp2002.mvps.org/hosts.htm
# The script will backup Ubuntu's original hosts file, it must be the original
# to not a modded one, this is insurance so if it all goes pear shaped you'll
# have a clean file escape route, just use as a template and copy it over.
# This script needs to run as root, or automate by dropping it in cron weekly
# and doing "sudo chmod 755 /etc/cron.weekly/hosts-update" in a terminal.
# In case you where wondering, apparently with "127.0.0.1" your browser will
# wait for a timeout hence the editing to "0.0.0.0" which will go straight to
# "404 not found" without waiting.

# EXCLUDES list for any sites you really want but are listed so won't load.
# To exclude a range just list the common part of the pattern, i.e. "666.com"
# will affect many of the addresses whereas "xs.666.co.ruok" is more exclusive,
# case matters by the way Google/google.
EXCLUDES="
troganviriidownload.doh
www.skankyhookers.ick
malwaredrivebyselfservice.eek
deaddodgysoftwarez.duh
google-analytics
googleadservices
adservices.google
adsenseshare.com
"                                                       # Don't block these.
URLHOSTS="http://winhelp2002.mvps.org/hosts.zip"        # Hosts download site.
BAKHOSTS="/etc/hosts.bak"                               # Insurance file.
INMAILTO="robz1@$HOSTNAME root@$HOSTNAME"               # Error/or not mail.

if [ ! -e "$BAKHOSTS" ]; then
    cp /etc/hosts "$BAKHOSTS"                           # Backup original file.
    chmod 444 "$BAKHOSTS"                               # Make it read only.
fi

cd /tmp

# Wget the new hosts file.
if [ ! -e "hosts.zip" ]; then wget -q -i -T 60 "$URLHOSTS"; fi
if [ -e "hosts.zip" ]; then
    echo -e "New hosts file installed, be aware:-\
    \nThis may affect web access to some previously ok websites." | \
    mail -s "[HOSTS FILE] Good download." $INMAILTO
    zenity --notification --text "New Hosts file - Check web access" & disown
else echo -e "Non critical: The weekly download of hosts.zip has failed.\
    \nRun hosts-update.sh as root to install the latest hosts file." | \
    mail -s "[HOSTS FILE] Failed download." $INMAILTO
    exit 1
fi

unzip -c "$(basename "$URLHOSTS")" HOSTS Hosts hosts 2>/dev/null > RAWHOSTS
sed -i 's/.$//' RAWHOSTS                                # Convert dos to unix.

# Create hybrid hosts file.
cat > /tmp/hosts << EOF
#-----------------------------------------------------------------------------
# This hosts block list was created on $(date)
# using the script "hosts-update" by robz.
# It combines the original Ubuntu hosts file saved as "$BAKHOSTS"
# and the latest the hosts file from: $URLHOSTS
#-----------------------------------------------------------------------------

# Start of default hostfile list text copied from "$BAKHOSTS"

$(cat $BAKHOSTS)

# End of default hostfile list text, what follows is supplemental.
#
# Any failure to connect to a previously ok website may be traced to an entry
# in the following list. Modifications to the list should be double hash ##
# commented out rather than deleted.
#
$(sed 's/127.0.0.1/0.0.0.0/g' RAWHOSTS | \
sed -ne '1,/of lines/!p' -e '1,/of entries/!p')

EOF

# Implement excludes list to comment out addresses you don't want to block.
for MODHOSTS in $EXCLUDES; do
sed -i '/'$MODHOSTS'/ s/^\([^## ]\)/## \1/g' hosts
done
mv -f hosts /etc/hosts                                   # Overwrite old hosts.

No comments: