Skip to content

Instantly share code, notes, and snippets.

@maxwelleite
Forked from Eliastik/update-hosts.sh
Created March 4, 2018 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxwelleite/ce3987b3f510f1216d0fe5a3d6c104ab to your computer and use it in GitHub Desktop.
Save maxwelleite/ce3987b3f510f1216d0fe5a3d6c104ab to your computer and use it in GitHub Desktop.
A quick shell script that will automatically update a Linux HOSTS file to block domains (ads, malwares, ...). Support multiple hosts sources and initial host file.
#!/bin/bash
# Filename: update-hosts.sh
#
# Author: George Lesica <george@lesica.com>
# Enhanced by Eliastik ( eliastiksofts.com/contact )
#
# Description: Replaces the HOSTS file with hosts lists from Internet,
# creating a backup of the old file. Can be used as an update script.
#
# Enhancement by Eliastik :
# Added the possibility to download multiple hosts files from multiple sources,
# added the possibility to use an initial host file to be appended at the top
# of the system host file, others fixes.
#
# Can be used as a cron script.
# Configuration variables:
# Add an host source by adding a space after the last entry of the variable HOSTS_URLS (before the ")"), then by adding your URL with quotes (ex: "http://www.example.com/hosts.txt")
HOSTS_URLS=( "http://someonewhocares.org/hosts/zero/hosts/" "https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext&useip=0.0.0.0" "http://winhelp2002.mvps.org/hosts.txt" )
INITIAL_HOSTS="/etc/hosts.initial"
NEW_HOSTS="hosts"
HOSTS_PATH="/etc/hosts"
NB_MAX_DOWNLOAD_RETRYING=10
# Check for root
if [ "$(id -u)" -ne "0" ]; then
echo "This script must be run as root. Exiting..." 1>&2
exit 1
fi
# Check curl
if ! [ -x "$(command -v curl)" ]; then
echo 'Error: curl is not installed. Please install it to run this script.' >&2
exit 1
fi
# create temporary directory
echo "Creating temporary directory..."
cd $(mktemp -d)
echo "Created temporary directory at $(pwd)"
# create new temp hosts
if [ -f "$INITIAL_HOSTS" ]
then
cat $INITIAL_HOSTS>$NEW_HOSTS
else
echo "The initial host file doesn't exist: $INITIAL_HOSTS"
echo "">$NEW_HOSTS
fi
# Print the update time
DATE=`date '+%Y-%m-%d %H:%M:%S'`
echo "">>$NEW_HOSTS
echo "# HOSTS last updated: $DATE">>$NEW_HOSTS
echo "#">>$NEW_HOSTS
# Grab hosts file
for i in "${HOSTS_URLS[@]}"
do
:
nberror=0
echo "Downloading hosts list from: $i"
while true; do
curl -s --fail "$i">>$NEW_HOSTS && break ||
nberror=$((nberror + 1))
echo "Download failed ! Retrying..."
if [ $nberror -ge $NB_MAX_DOWNLOAD_RETRYING ]; then
echo "Download failed $NB_MAX_DOWNLOAD_RETRYING time(s). Check your Internet connection and the host source then try again. Exiting..."
exit 1;
fi
done
done
# Backup old hosts file
echo "Backup old hosts file..."
cp -v $HOSTS_PATH ${HOSTS_PATH}.bak
echo "Installing hosts list..."
cp -v $NEW_HOSTS $HOSTS_PATH
# Clean up old downloads
echo "Removing cache..."
rm $NEW_HOSTS*
echo "Done !"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment