Old:UpHosts: differenze tra le versioni

m
ha spostato UpHosts a Old:UpHosts
(1st revision)
 
m (ha spostato UpHosts a Old:UpHosts)
 
(11 versioni intermedie di 4 utenti non mostrate)
Riga 1: Riga 1:
{{Old}}
==Introduzione==
==Introduzione==
{{ Warningbox | IN ALLESTIMENTO }}
{{ Warningbox | questo script è stato creato per uso personale, in parte riadattando e modificando script altrui
(ringrazio tutti i rispettivi autori) e nonostante abbia cercato di testarlo al meglio, non posso offrire alcuna garanzia :-) }}
==Descrizione==
Premessa: esistono svariati sistemi di content filtering decisamente più raffinati e personalizzabili di questo.
Premessa: esistono svariati sistemi di content filtering decisamente più raffinati e personalizzabili di questo.


Riga 11: Riga 6:


Tali host vengono inseriti in /etc/hosts come corrispondenti a 0.0.0.0 , bloccandone di fatto l'accesso.
Tali host vengono inseriti in /etc/hosts come corrispondenti a 0.0.0.0 , bloccandone di fatto l'accesso.
{{ Warningbox | questo script è stato creato per uso personale, in parte riadattando e modificando script altrui
(ringrazio tutti i rispettivi autori) e nonostante abbia cercato di testarlo al meglio, non posso offrire alcuna garanzia :-) }}
[[Utente:Paolo321|Paolo321]] , 31 dicembre 2010




==Funzionamento==
==Funzionamento==
Spero che lo script sia sufficientemente chiaro e si "spieghi da solo", comunque lo script esegue:
Spero che lo script sia sufficientemente chiaro e si "spieghi da solo", comunque lo script esegue:
*Controllo presenza software richiesti
*Controllo privilegi di root e presenza software richiesti
*Controllo di quanto tempo è passato dall'ultima esecuzione (per evitare download troppo frequenti, vedi esempio sotto)
*Backup dell'hosts file presistente (se non già effettuato)
*Backup dell'hosts file presistente (se non già effettuato)
*Download delle liste
*Download delle liste
Riga 29: Riga 30:
**Eventuali modifiche permanenti al file hosts andranno quindi effettuate non in /etc/hosts ma nel file hosts di backup
**Eventuali modifiche permanenti al file hosts andranno quindi effettuate non in /etc/hosts ma nel file hosts di backup
*Report numero hosts bloccati e uscita dello script
*Report numero hosts bloccati e uscita dello script
==Note==
*Come default lo script cerca blacklist e whitelist locali nella sua directory, quindi si può ad esempio collocare il tutto in /opt/uphosts. Ovviamente si può cambiare i path nello script e usarne altri.
*Uno script di questo tipo è comodo sia eseguito "unattended"; iIl wrapper zzz-uphosts-run esegue uphosts e reindirizza l'output nei log di sistema. Può ad esempio essere eseguito da /etc/network/if-up.d, oppure tramite cron (ma in questo secondo caso c'è da aggiungere un controllo sulla presenza di connessione internet). Lo script all'avvio controlla comunque quanto tempo è passato dall'ultima esecuzione (per evitare che ad esempio dieci connessioni-disconnessioni al giorno portino a dieci download delle liste)




==Script==
==Script==
Come default lo script cerca le due whitelist locali nella sua directory, quindi si può ad esempio collocare il tutto in /opt/uphosts.


Ovviamente si può cambiare i path nello script e usarne altri.
===uphosts.sh===
===uphosts.sh===
<pre>
<pre>
#!/bin/bash
#!/bin/bash


# uphosts - Hosts file updater
# uphosts - Hosts File Updater


# README:
# README:
Riga 52: Riga 56:
# http://hostsfile.mine.nu/downloads/updatehosts.sh.txt
# http://hostsfile.mine.nu/downloads/updatehosts.sh.txt


# 20100721 Paolo
# 20101216 Paolo


#-----------------------------------------------------------------------
#-----------------------------------------------------------------------
#  VARIABLES -----------------------------------------------------------


HOSTSPATH="/tmp/hosts-`date +%s`" # Temp directory
HOSTSPATH="/tmp/hosts-`date +%s`" # Temp directory
HOSTSFILE="/etc/hosts" # Hosts file
HOSTSFILE="/etc/hosts" # Hosts file
ORIGFILE="$HOSTSFILE.original" # Backup file
ORIGFILE="$HOSTSFILE.original" # Backup file


CONFDIR="."
CONFDIR="$(dirname $(readlink -f $0))" # Parent directory of the script
BLACKLIST="$CONFDIR/uphosts-blacklist" # Local Blacklist
BLACKLIST="$CONFDIR/uphosts-blacklist" # Local Blacklist
WHITELIST="$CONFDIR/uphosts-whitelist" # Whitelist
WHITELIST="$CONFDIR/uphosts-whitelist" # Whitelist
Riga 69: Riga 71:
PROXYPASS="" #PROXYPASS="--proxy-password='password"
PROXYPASS="" #PROXYPASS="--proxy-password='password"


# STARTING ------------------------------------------------------------
DAYS="2" # Update frequency
 
#-----------------------------------------------------------------------


echo ""
# Checks for root privileges
echo "--------------------------------"
if [ "$(whoami)" != 'root' ] ; then
echo "- uphosts - Hosts File Updater -"
echo "You need to be root to execute uphosts. Exiting!"
echo "--------------------------------"
exit 1
echo ""
fi


echo "Checking for required applications ..."; ABORT=0
# Checks required packages
builtin type -P wget    &>/dev/null || { echo "wget is missing."; ABORT=1; }
ABORT=0
builtin type -P unzip    &>/dev/null || { echo "unzip is missing."; ABORT=1; }
builtin type -P wget    &>/dev/null || { echo -n "wget is missing."; ABORT=1; }
builtin type -P fromdos  &>/dev/null || { echo "fromdos is missing."; ABORT=1; }
builtin type -P unzip    &>/dev/null || { echo -n "unzip is missing."; ABORT=1; }
builtin type -P grep    &>/dev/null || { echo "grep is missing."; ABORT=1; }
builtin type -P fromdos  &>/dev/null || { echo -n "fromdos(tofrodos) is missing."; ABORT=1; }
builtin type -P grep    &>/dev/null || { echo -n "grep is missing."; ABORT=1; }


if [ $ABORT != 0 ] ; then
if [ $ABORT != 0 ] ; then
echo "Exiting!"
echo " Exiting!"
exit 1
exit 2
fi
fi


echo "OK"
# Limits updates if uphosts is run often (i.e. at every if-up)
# If there is no original hosts file this is the first run on a fresh system, and update runs anyway
if [ -f "$ORIGFILE" ] && [ `find $HOSTSFILE -mtime -$DAYS` ] ; then
echo "$HOSTSFILE is less than $DAYS days old. Exiting!"
exit 3
fi


# If there is no original hosts file this is the first run on a fresh system
# (as above, but now original hosts file is saved)
if [ ! -f "$ORIGFILE" ] ; then
if [ ! -f "$ORIGFILE" ] ; then
echo "Backing up your previous hosts file ..."
echo "Backing up your previous hosts file ..."
cp $HOSTSFILE $ORIGFILE
cp $HOSTSFILE $ORIGFILE
echo "OK"
#echo "OK"
fi
fi
#-----------------------------------------------------------------------


mkdir $HOSTSPATH
mkdir $HOSTSPATH


# DOWNLOADING ---------------------------------------------------------
# Every list has some specific tweaks :-)
#  ... Every list has some specific tweaks :-)
# If there are any errors, the script exits. Merging lists partially could be unsafe


#### hphosts list
#### hphosts list
Riga 107: Riga 121:
HFILE1INT="HOSTS.txt" # Needed to specify which file from zip
HFILE1INT="HOSTS.txt" # Needed to specify which file from zip
# Downloading
# Downloading
echo "Retrieving $HFNAME1 from $HFSERVER1 ..."
echo -n "Retrieving $HFNAME1 from $HFSERVER1 ..."
wget -q -O $HOSTSPATH/$HFNAME1 $HFSERVER1/$HFILE1 $PROXYUSER $PROXYPASS
wget -q -O $HOSTSPATH/$HFNAME1 $HFSERVER1/$HFILE1 $PROXYUSER $PROXYPASS || { echo " ERROR! Exiting!"; exit 11; }
unzip -p $HOSTSPATH/$HFNAME1 $HFILE1INT | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME1
unzip -p $HOSTSPATH/$HFNAME1 $HFILE1INT | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME1
echo "OK"
echo " OK"


#### hphosts-partial list
#### hphosts-partial list
Riga 117: Riga 131:
HFILE2="hphosts-partial.asp"
HFILE2="hphosts-partial.asp"
# Downloading
# Downloading
echo "Retrieving $HFNAME2 from $HFSERVER2 ..."
echo -n "Retrieving $HFNAME2 from $HFSERVER2 ..."
wget -q -O $HOSTSPATH/$HFNAME2 $HFSERVER2/$HFILE2 $PROXYUSER $PROXYPASS
wget -q -O $HOSTSPATH/$HFNAME2 $HFSERVER2/$HFILE2 $PROXYUSER $PROXYPASS || { echo " ERROR! Exiting!"; exit 12; }
cat $HOSTSPATH/$HFNAME2 | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME2
cat $HOSTSPATH/$HFNAME2 | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME2
echo "OK"
echo " OK"


#### MVPs list
#### MVPs list
Riga 128: Riga 142:
HFILE3INT="HOSTS" # Needed to specify which file from zip
HFILE3INT="HOSTS" # Needed to specify which file from zip
# Downloading
# Downloading
echo "Retrieving $HFNAME3 from $HFSERVER3 ..."
echo -n "Retrieving $HFNAME3 from $HFSERVER3 ..."
wget -q -O $HOSTSPATH/$HFNAME3 $HFSERVER3/$HFILE3 $PROXYUSER $PROXYPASS
wget -q -O $HOSTSPATH/$HFNAME3 $HFSERVER3/$HFILE3 $PROXYUSER $PROXYPASS || { echo " ERROR! Exiting!"; exit 13; }
unzip -p $HOSTSPATH/$HFNAME3 $HFILE3INT | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d'  -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME3
unzip -p $HOSTSPATH/$HFNAME3 $HFILE3INT | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d'  -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME3
echo "OK"
echo " OK"


#### hostsfile.mine.nu, 0.0.0.0 format
#### hostsfile.mine.nu, 0.0.0.0 format
Riga 138: Riga 152:
#HFILE4="hosts0.zip"
#HFILE4="hosts0.zip"
## Downloading
## Downloading
#echo "Retrieving $HFNAME4 from $HFSERVER4 ..."
#echo -n "Retrieving $HFNAME4 from $HFSERVER4 ..."
#wget -q -O $HOSTSPATH/$HFNAME4 $HFSERVER4/$HFILE4 $PROXYUSER $PROXYPASS
#wget -q -O $HOSTSPATH/$HFNAME4 $HFSERVER4/$HFILE4 $PROXYUSER $PROXYPASS || { echo " ERROR! Exiting!"; exit 14; }
#unzip -p $HOSTSPATH/$HFNAME4 | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' > $HOSTSPATH/hosts-$HFNAME4
#unzip -p $HOSTSPATH/$HFNAME4 | fromdos | grep -v localhost | sed -e '/#.*/ d' -e '/^$/ d' > $HOSTSPATH/hosts-$HFNAME4
#echo "OK"
#echo " OK"


# PROCESSING ----------------------------------------------------------
#-----------------------------------------------------------------------


echo "Processing local blacklist $BLACKLIST ..."
echo -n "Processing local blacklist $BLACKLIST ..."
if [ -f "$BLACKLIST" ] ; then
if [ -f "$BLACKLIST" ] ; then
cat $BLACKLIST | sed -e '/#.*/ d' -e '/^$/ d' -e 's/^/0.0.0.0 /g' > $HOSTSPATH/blacklist.ready
cat $BLACKLIST | sed -e '/#.*/ d' -e '/^$/ d' -e 's/^/0.0.0.0 /g' > $HOSTSPATH/blacklist.ready
echo "OK"
echo " OK"
echo "Merging lists ..."
echo -n "Merging lists ..."
#cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/hosts-$HFNAME4 $HOSTSPATH/blacklist.ready | sort | uniq > $HOSTSPATH/hosts.all
#cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/hosts-$HFNAME4 $HOSTSPATH/blacklist.ready | sort | uniq > $HOSTSPATH/hosts.all
cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/blacklist.ready | sort | uniq > $HOSTSPATH/hosts.all
cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/blacklist.ready | sort | uniq > $HOSTSPATH/hosts.all
echo "OK"
echo " OK"
else
else
echo "NOT FOUND"
echo " NOT FOUND"
echo "Merging lists ..."
echo -n "Merging lists ..."
#cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/hosts-$HFNAME4 | sort | uniq > $HOSTSPATH/hosts.all
#cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 $HOSTSPATH/hosts-$HFNAME4 | sort | uniq > $HOSTSPATH/hosts.all
cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 | sort | uniq > $HOSTSPATH/hosts.all
cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 | sort | uniq > $HOSTSPATH/hosts.all
echo "OK"
echo " OK"
fi
fi


echo "Processing whitelist $WHITELIST ..."
echo -n "Processing whitelist $WHITELIST ..."
if [ -f "$WHITELIST" ] ; then
if [ -f "$WHITELIST" ] ; then
cat $WHITELIST | sed -e '/#.*/ d' -e '/^$/ d' > $HOSTSPATH/whitelist.ready
cat $WHITELIST | sed -e '/#.*/ d' -e '/^$/ d' > $HOSTSPATH/whitelist.ready
grep -Fvf $HOSTSPATH/whitelist.ready $HOSTSPATH/hosts.all > $HOSTSPATH/hosts.all.2
grep -Fvf $HOSTSPATH/whitelist.ready $HOSTSPATH/hosts.all > $HOSTSPATH/hosts.all.2
mv $HOSTSPATH/hosts.all.2 $HOSTSPATH/hosts.all
mv $HOSTSPATH/hosts.all.2 $HOSTSPATH/hosts.all
echo "OK"
echo " OK"
else
else
echo "NOT FOUND"
echo " NOT FOUND"
fi
fi


echo "Writing hosts file $HOSTSFILE ..."
#-----------------------------------------------------------------------


echo -n "Writing hosts file $HOSTSFILE ..."
cat $ORIGFILE > $HOSTSFILE
cat $ORIGFILE > $HOSTSFILE
echo "OK"
echo " OK"


HOSTCOUNT=`cat $HOSTSPATH/hosts.all | wc -l`
HOSTCOUNT=`cat $HOSTSPATH/hosts.all | wc -l`
Riga 182: Riga 197:


cat >> $HOSTSFILE << EOF
cat >> $HOSTSFILE << EOF
#=============================================================
#==================================================================
#
#
# `date`
# `date`
# $HOSTCOUNT hosts blocked by uphosts
# $HOSTCOUNT hosts blocked by uphosts
#
#
# Original file: $ORIGFILE
# Original file: $ORIGFILE
# Permanent changes can be done there...
# Permanent changes can be done there, it is imported at every run
# It is imported (see above) at every run
#
#
#=============================================================
#==================================================================
EOF
EOF


Riga 199: Riga 213:
#rm -fv $HOSTSPATH/hosts*
#rm -fv $HOSTSPATH/hosts*
echo "Update process complete - $HOSTCOUNT hosts blocked!"
echo "Update process complete - $HOSTCOUNT hosts blocked!"
</pre>
===zzz-uphosts-run===
<pre>
#!/bin/sh
# uphosts - Hosts File Updater
# zzz-uphosts-run
# wrapper for running uphosts logging output
# for example, from /etc/network/if-up.d
# In that case, this script is run by run-parts
# Check run-parts naming conventions: "must consist entirely of upper and lower case letters, digits, underscores, and hyphens"
# ie do not name this script foo.sh !!!
# 20101230 Paolo
UPHOSTSFILE="/opt/uphosts/uphosts.sh"
LOGGERPARAMS="-t uphosts"
$UPHOSTSFILE | logger $LOGGERPARAMS &
</pre>
</pre>


Riga 228: Riga 264:
# test12345.com
# test12345.com
# test67890.com
# test67890.com
</pre>
</pre>
3 581

contributi