Skip to content

Instantly share code, notes, and snippets.

@Ceesaxp
Forked from Eliastik/update-hosts.sh
Created April 23, 2021 10:56
Show Gist options
  • Select an option

  • Save Ceesaxp/d39546311b3f7e4f7ab75bc91d9171ec to your computer and use it in GitHub Desktop.

Select an option

Save Ceesaxp/d39546311b3f7e4f7ab75bc91d9171ec 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, initial host file and incorrect/malicious entries checking.
#!/bin/bash
# Filename: update-hosts.sh
#
# Author: George Lesica <george@lesica.com>
# Enhanced by Eliastik ( eliastiksofts.com/contact )
# Version 1.2 (18 may 2020) - Eliastik
#
# 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 hosts file to be appended at the top
# of the system hosts file, added a possibility to uninstall and restore
# the hosts file, added malicious entries checking, others fixes.
#
# Can be used as a cron script.
#
# Launch arguments:
# - Without arguments (./update-hosts.sh), the script update the hosts file
# - With restore (./update-hosts.sh restore), the script restore the backup hosts file if it exists
# - With uninstall (./update-hosts.sh uninstall), the script uninstalls the hosts file and restore only the initial hosts file
# - With check (./update-hosts.sh check), the script checks the hosts file for incorrect or malicious entries
# Configuration variables:
# Add an hosts 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=( "https://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_RETRY=10
CHECK_HOSTS=1 # 1 for checking for malicious entries, 0 to disable
function check_hosts() {
echo "Checking hosts data..."
lineNumber=1
while IFS= read -r line
do
lineNumber=$((lineNumber + 1))
first_word="$(echo "$line" | head -n1 | tr '\t' ' ' | tr '\r' ' ' | cut -d " " -f1)"
first_letter="$(echo $first_word | head -c 1)"
if [ -n "${first_word// }" ] && [ "$first_letter" != "#" ] && [ "$first_word" != "127.0.0.1" ] && [ "$first_word" != "0.0.0.0" ] && [ "$first_word" != "255.255.255.255" ] && [[ "$first_word" != "::1"* ]] && [[ "$first_word" != "fe00::"* ]] && [[ "$first_word" != "ff0"*"::"* ]]
then
echo "Found incorrect or malicious entry. Exiting..."
echo "Entry found: '${line}' at line $lineNumber"
return 1
fi
done < "$1"
echo "No incorrect or malicious entry found."
return 0
}
# 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
# Check for arguments - restore or uninstall the hosts file
if [ $# -ge 1 ]; then
if [ "$1" = "restore" ]; then
echo "Restoring your hosts file backup..."
if [ -f "${HOSTS_PATH}.bak" ]; then
cp -v ${HOSTS_PATH}.bak $HOSTS_PATH
echo "Done !"
exit 1
else
echo "The backup hosts file doesn't exist: ${HOSTS_PATH}.bak"
echo "Exiting..."
exit 1
fi
fi
if [ "$1" = "uninstall" ]; then
echo "Uninstalling your hosts file and restoring initial hosts file..."
if [ -f "$INITIAL_HOSTS" ]; then
cp -v $INITIAL_HOSTS $HOSTS_PATH
echo "Done !"
exit 1
else
echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
echo "Exiting..."
exit 1
fi
fi
if [ "$1" = "check" ]; then
if [ -f "${HOSTS_PATH}" ]; then
check_hosts $HOSTS_PATH
exit 1
else
echo "The hosts file doesn't exist: $HOSTS_PATH"
echo "Exiting..."
exit 1
fi
fi
fi
# create temporary directory
echo "Creating temporary directory..."
cd $(mktemp -d)
echo "Created temporary directory at $(pwd)"
# create new temp hosts
echo "">$NEW_HOSTS
# 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_RETRY ]; then
echo "Download failed $NB_MAX_DOWNLOAD_RETRY time(s). Check your Internet connection and the hosts 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
if ! [ -f "${HOSTS_PATH}.bak" ]; then
echo "HOSTS file backup not created. Exiting securely..."
exit 1
fi
# Checking new hosts
if [ "$CHECK_HOSTS" = "1" ]
then
check_hosts $NEW_HOSTS || ( echo "You can disable hosts checking by changing the value of the variable CHECK_HOSTS to 0 in the script." && exit 1 )
fi
# Install hosts
echo "Installing hosts list..."
# Copy initial hosts
if [ -f "$INITIAL_HOSTS" ]; then
cat $INITIAL_HOSTS>$HOSTS_PATH
else
echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
echo "">$HOSTS_PATH
fi
cat $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