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.

# 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,
# also some more reading and the
# zipped source file
# 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 "" your browser will
# wait for a timeout hence the editing to "" 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. ""
# will affect many of the addresses whereas "" is more exclusive,
# case matters by the way Google/google.
"                                                       # Don't block these.
URLHOSTS=""        # 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.

cd /tmp

# Wget the new hosts file.
if [ ! -e "" ]; then wget -q -i -T 60 "$URLHOSTS"; fi
if [ -e "" ]; 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 has failed.\
    \nRun as root to install the latest hosts file." | \
    mail -s "[HOSTS FILE] Failed download." $INMAILTO
    exit 1

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/' RAWHOSTS | \
sed -ne '1,/of lines/!p' -e '1,/of entries/!p')


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

No comments: