Autore Topic: script hosts file per Linux  (Letto 1678 volte)

0 Utenti e 1 Visitatore stanno visualizzando questo topic.

Offline paolo321

  • Hero Member
  • *****
  • Post: 542
  • Karma: 8
script hosts file per Linux
« il: 14/06/2010, 11:23 »
Notato che non esiste qualcosa tipo HostsMan, ho provato a mettere insieme qualcosa (ho preso diverse parti di codice da script preesistenti, e citati per correttezza nello script..)
Dovrebbe gestire update e merge di una serie di liste...

E' tutto da provare, quindi non mi assumo responsabilità :-) , ma ci tengo a condividerlo sperando nei vostri suggerimenti :-)

Codice: [Seleziona]
#!/bin/bash

# uphosts - Hosts file updater

# Sources:
# http://ubuntuedintorni.wordpress.com/2009/06/29/di-script-dns-e-file-host/
# http://hostsfile.mine.nu/downloads/updatehosts.sh.txt

# 20100613 Paolo - Initial Version
#-----------------------------------------------------------------------


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

HOSTSPATH="/tmp/hosts-`date +%s`" # Temp directory
HOSTSFILE="/etc/hosts"        # Hosts file
ORIGFILE="$HOSTSFILE.original"           # Backup file
PROXYUSER=""
PROXYPASS=""
# If needed:
#PROXYUSER="--proxy-user=user.name"
#PROXYPASS="--proxy-password='password"


#  SOURCES -------------------------------------------------------------
# Every source has at least a name and a url/filename for wget

# hostsfile.mine.nu, 0.0.0.0 format
HFNAME1="mine-nu-0"
HFSERVER1="http://hostsfile.mine.nu.nyud.net"
HFILE1="hosts0.zip"

# hphosts list
HFNAME2="hphosts"
HFSERVER2="http://support.it-mate.co.uk/downloads"
HFILE2="hphosts.zip"
HFILE2INT="HOSTS.txt" # File to extract from zip

# MVPs list
HFNAME3="mvps"
HFSERVER3="http://www.mvps.org/winhelp2002"
HFILE3="hosts.zip"
HFILE3INT="HOSTS" # File to extract from zip


#  STARTING ------------------------------------------------------------

echo ""
echo "--------------------------------"
echo "- uphosts - Hosts File Updater -"
echo "--------------------------------"
echo ""

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

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

echo "OK"

if [ ! -f "$ORIGFILE" ] ; then
  echo "Backing up your previous hosts file ..."
  cp $HOSTSFILE $ORIGFILE
  echo "OK"
fi

mkdir $HOSTSPATH # Why needed ???

#  DOWNLOADING ---------------------------------------------------------
# Every list may need some specific tweaks :-)

echo "Retrieving $HFNAME1 from $HFSERVER1 ..."
wget -q -O $HOSTSPATH/$HFILE1 $HFSERVER1/$HFILE1 $PROXYUSER $PROXYPASS
#unzip -p $HOSTSPATH/$HFILE1 | fromdos | grep -v localhost | grep -v '^#' | grep -v '^$' > $HOSTSPATH/hosts-$HFNAME1
unzip -p $HOSTSPATH/$HFILE1 | fromdos | grep -v localhost | sed -e 's/#.*//' -e '/^$/ d' > $HOSTSPATH/hosts-$HFNAME1
echo "OK"

echo "Retrieving $HFNAME2 from $HFSERVER2 ..."
wget -q -O $HOSTSPATH/$HFILE2 $HFSERVER2/$HFILE2 $PROXYUSER $PROXYPASS
unzip -p $HOSTSPATH/$HFILE2 $HFILE2INT | fromdos | grep -v localhost | sed -e 's/#.*//' -e '/^$/ d' -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME2
echo "OK"

echo "Retrieving $HFNAME3 from $HFSERVER3 ..."
wget -q -O $HOSTSPATH/$HFILE3 $HFSERVER3/$HFILE3 $PROXYUSER $PROXYPASS
unzip -p $HOSTSPATH/$HFILE3 $HFILE3INT | fromdos | grep -v localhost | sed -e 's/#.*//' -e '/^$/ d'  -e 's/127.0.0.1/0.0.0.0/g' > $HOSTSPATH/hosts-$HFNAME3
echo "OK"


#  FINISHING -----------------------------------------------------------

echo "Merging lists ..."
cat $HOSTSPATH/hosts-$HFNAME1 $HOSTSPATH/hosts-$HFNAME2 $HOSTSPATH/hosts-$HFNAME3 | sort | uniq > $HOSTSPATH/hosts.all
echo "OK"

echo "Adding original hosts file $ORIGFILE ..."
cat $ORIGFILE > $HOSTSFILE
echo "OK"

HOSTCOUNT=`cat $HOSTSPATH/hosts.all | wc -l`

echo "" >> $HOSTSFILE # to make sure the original file ends in a new-line
echo "" >> $HOSTSFILE
echo "#============================================================="   >> $HOSTSFILE
echo "# Modified by uphosts - Backup is $ORIGFILE"                      >> $HOSTSFILE
echo "# Persistent changes should be made to $ORIGFILE,"                >> $HOSTSFILE
echo "# since it is imported every time when updating"                  >> $HOSTSFILE
echo "#"                                                                >> $HOSTSFILE
echo "# `date`"                                                         >> $HOSTSFILE
echo "# $HOSTCOUNT hosts blocked"                                       >> $HOSTSFILE
echo "#"                                                                >> $HOSTSFILE
echo "#============================================================="   >> $HOSTSFILE

cat $HOSTSPATH/hosts.all >> $HOSTSFILE

#rm -fv $HOSTSPATH/hosts*
echo ""
echo "Update process complete - $HOSTCOUNT hosts blocked!"

#echo "As a side-effect of this script, any changes you wish to make"
#echo "persistent in the hosts file should be made to $ORIGFILE"
#echo "because $HOSTSFILE will be respawned from that file and the "
#echo "new list from the server each time this script runs."