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.

Revisions

  1. @Eliastik Eliastik revised this gist Apr 22, 2021. 1 changed file with 36 additions and 6 deletions.
    42 changes: 36 additions & 6 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.2.1 (19 may 2020) - Eliastik
    # Version 1.3 (22 april 2021) - 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.
    @@ -13,7 +13,8 @@
    # 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 incorrect/malicious entries checking, others fixes.
    # the hosts file, added incorrect/malicious entries checking,
    # added the possibility to exclude hosts filtering for specific domains, others fixes.
    #
    # Can be used as a cron script.
    #
    @@ -26,12 +27,13 @@
    # 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" "https://winhelp2002.mvps.org/hosts.txt" )
    INITIAL_HOSTS="/etc/hosts.initial"
    NEW_HOSTS="hosts"
    HOSTS_PATH="/etc/hosts"
    HOSTS_PATH="/etc/hosts" # The path to the hosts file
    INITIAL_HOSTS="/etc/hosts.initial" # The initial host file to be appended at the top of the hosts file
    EXCLUDE_HOSTS="/etc/hosts.exclude" # A file containing a list of domain to be excluded from the hosts file (1 domain per line)
    NEW_HOSTS="hosts" # New host name
    NB_MAX_DOWNLOAD_RETRY=10
    CHECK_HOSTS=0 # 1 for checking for malicious entries, 0 to disable
    GOOD_HOSTS=( "127."*"."*"."* "10."*"."*"."* "0.0.0.0" "255.255.255.255" "::1" "fe"*"::"* "ff0"*"::"* )
    GOOD_HOSTS=( "127."*"."*"."* "10."*"."*"."* "0.0.0.0" "255.255.255.255" "::1" "fe"*"::"* "ff0"*"::"* ) # A list of safe hosts

    function check_hosts() {
    echo "Checking hosts data..."
    @@ -178,6 +180,34 @@ if ! [ -f "${HOSTS_PATH}.bak" ]; then
    exit 1
    fi

    # Exclude hosts (from EXCLUDE_HOSTS file)
    if [ -f "$EXCLUDE_HOSTS" ]; then
    echo "Excluding hosts..."
    lineNumber=0
    linesHost=$(cat "$NEW_HOSTS" | tr '\t' ' ' | tr '\r' ' ')
    linesHostExcluded=$(cat "$EXCLUDE_HOSTS" | tr '\t' ' ' | tr '\r' ' ')

    while read -r lineHost; do
    lineNumber=$((lineNumber + 1))
    excludedHost=0
    fileHost=$(echo "$lineHost" | tr '\t' ' ' | tr '\r' ' ' | sed -e 's/^[[:space:]]*//' | cut -d " " -f2 | head -n2)

    while read -r lineExclude; do
    if [[ "$fileHost" == $lineExclude ]]; then
    echo "Excluded '${lineHost}' (line $lineNumber)"
    excludedHost=1
    fi
    done <<< $linesHostExcluded

    if [ "$excludedHost" = "0" ]; then
    echo "$lineHost">>$NEW_HOSTS".tmp"
    fi
    done <<< $linesHost

    echo "">$NEW_HOSTS
    cat $NEW_HOSTS".tmp">>$NEW_HOSTS
    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 )
  2. @Eliastik Eliastik revised this gist Dec 23, 2020. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -25,12 +25,12 @@

    # 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" )
    HOSTS_URLS=( "https://someonewhocares.org/hosts/zero/hosts" "https://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext&useip=0.0.0.0" "https://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
    CHECK_HOSTS=0 # 1 for checking for malicious entries, 0 to disable
    GOOD_HOSTS=( "127."*"."*"."* "10."*"."*"."* "0.0.0.0" "255.255.255.255" "::1" "fe"*"::"* "ff0"*"::"* )

    function check_hosts() {
  3. @Eliastik Eliastik revised this gist May 19, 2020. No changes.
  4. @Eliastik Eliastik revised this gist May 19, 2020. 1 changed file with 72 additions and 38 deletions.
    110 changes: 72 additions & 38 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.2 (18 may 2020) - Eliastik
    # Version 1.2.1 (19 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.
    @@ -13,15 +13,15 @@
    # 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.
    # the hosts file, added incorrect/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
    # - With uninstall (./update-hosts.sh uninstall), the script uninstall the hosts file and restore only the initial hosts file
    # - With check (./update-hosts.sh check), the script check the hosts file for incorrect or malicious entries (no root needed)

    # 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")
    @@ -31,48 +31,61 @@ NEW_HOSTS="hosts"
    HOSTS_PATH="/etc/hosts"
    NB_MAX_DOWNLOAD_RETRY=10
    CHECK_HOSTS=1 # 1 for checking for malicious entries, 0 to disable
    GOOD_HOSTS=( "127."*"."*"."* "10."*"."*"."* "0.0.0.0" "255.255.255.255" "::1" "fe"*"::"* "ff0"*"::"* )

    function check_hosts() {
    echo "Checking hosts data..."
    lineNumber=1
    lineNumber=0
    fileLines=$(cat $HOSTS_PATH | tr '\t' ' ' | tr '\r' ' ' | sed -e 's/^[[:space:]]*//' | cut -d " " -f1)

    while IFS= read -r line
    do
    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
    found=0
    first_word="$(echo "$line" | head -n1)"
    first_letter="$(echo "$first_word" | head -c 1)"

    if [ -n "${first_word// }" ] && [ "$first_letter" != "#" ]; then
    for i in "${GOOD_HOSTS[@]}"; do
    if [[ "$first_word" == $i ]]; then
    found=1
    fi
    done

    if [ "$found" = "0" ]; then
    echo "Found incorrect or malicious entry. Exiting..."
    echo "Entry found: '${line}' at line $lineNumber"
    return 1
    fi
    fi
    done < "$1"
    done <<< "$fileLines"

    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
    function check_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
    function 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
    check_root
    echo "Restoring your hosts file backup..."
    if [ -f "${HOSTS_PATH}.bak" ]; then
    cp -v ${HOSTS_PATH}.bak $HOSTS_PATH
    echo "Done !"
    exit 1
    echo "Done!"
    exit 0
    else
    echo "The backup hosts file doesn't exist: ${HOSTS_PATH}.bak"
    echo "Exiting..."
    @@ -81,11 +94,12 @@ if [ $# -ge 1 ]; then
    fi

    if [ "$1" = "uninstall" ]; then
    check_root
    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
    echo "Done!"
    exit 0
    else
    echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
    echo "Exiting..."
    @@ -95,8 +109,8 @@ if [ $# -ge 1 ]; then

    if [ "$1" = "check" ]; then
    if [ -f "${HOSTS_PATH}" ]; then
    check_hosts $HOSTS_PATH
    exit 1
    check_hosts $HOSTS_PATH || exit 1
    exit 0
    else
    echo "The hosts file doesn't exist: $HOSTS_PATH"
    echo "Exiting..."
    @@ -105,12 +119,29 @@ if [ $# -ge 1 ]; then
    fi
    fi

    # create temporary directory
    check_root # Check for root
    check_curl # Check for curl

    # Check for hosts file
    if [ ! -f "${HOSTS_PATH}" ]; then
    echo "The hosts file doesn't exist: $HOSTS_PATH"
    echo "Exiting..."
    exit 1
    fi

    # Create temporary directory
    echo "Creating temporary directory..."
    cd $(mktemp -d)
    TEMP_DIR=`mktemp -d`

    if [[ ! "$TEMP_DIR" || ! -d "$TEMP_DIR" ]]; then
    echo "The temporary directory could not have been created. Exiting securely..."
    exit 1
    fi

    cd "$TEMP_DIR"
    echo "Created temporary directory at $(pwd)"

    # create new temp hosts
    # Create new temp hosts
    echo "">$NEW_HOSTS

    # Print the update time
    @@ -125,28 +156,30 @@ 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;
    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
    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

    @@ -166,4 +199,5 @@ cat $NEW_HOSTS >> $HOSTS_PATH
    # Clean up old downloads
    echo "Removing cache..."
    rm $NEW_HOSTS*
    echo "Done !"
    echo "Done!"
    exit 0
  5. @Eliastik Eliastik revised this gist May 18, 2020. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.1.1 (18 may 2020) - Eliastik
    # 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.
  6. @Eliastik Eliastik revised this gist May 18, 2020. 1 changed file with 61 additions and 15 deletions.
    76 changes: 61 additions & 15 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.1.1.1 (17 august 2019) - Eliastik
    # Version 1.1.1 (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.
    @@ -13,22 +13,45 @@
    # 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, others fixes.
    # 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 uninstall the hosts file and restore only the initial hosts file
    # - 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" "http://pgl.yoyo.org/adservers/serverlist.php?showintro=0&mimetype=plaintext&useip=0.0.0.0" "http://winhelp2002.mvps.org/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_RETRYING=10
    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
    @@ -38,7 +61,7 @@ fi

    # Check curl
    if ! [ -x "$(command -v curl)" ]; then
    echo 'Error: curl is not installed. Please install it to run this script.' >&2
    echo "Error: curl is not installed. Please install it to run this script." >&2
    exit 1
    fi

    @@ -69,6 +92,17 @@ if [ $# -ge 1 ]; then
    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
    @@ -77,12 +111,7 @@ 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 hosts file doesn't exist: $INITIAL_HOSTS"
    echo "">$NEW_HOSTS
    fi
    echo "">$NEW_HOSTS

    # Print the update time
    DATE=`date '+%Y-%m-%d %H:%M:%S'`
    @@ -100,8 +129,8 @@ 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 hosts source then try again. Exiting..."
    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
    @@ -114,8 +143,25 @@ 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..."
    cp -v $NEW_HOSTS $HOSTS_PATH

    # 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..."
  7. @Eliastik Eliastik revised this gist Aug 17, 2019. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.1.1 (23 april 2018) - Eliastik
    # Version 1.1.1.1 (17 august 2019) - 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.
    @@ -24,7 +24,7 @@

    # 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" )
    HOSTS_URLS=( "https://someonewhocares.org/hosts/zero/hosts" "http://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"
  8. @Eliastik Eliastik revised this gist Jul 19, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -24,7 +24,7 @@

    # 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=( "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" )
    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"
  9. @Eliastik Eliastik revised this gist Apr 23, 2018. 1 changed file with 11 additions and 2 deletions.
    13 changes: 11 additions & 2 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.1 (16 april 2018)
    # Version 1.1.1 (23 april 2018) - 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.
    @@ -16,6 +16,11 @@
    # the hosts file, 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 uninstall the hosts file and restore only the initial hosts file

    # 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")
    @@ -51,7 +56,7 @@ if [ $# -ge 1 ]; then
    exit 1
    fi
    fi

    if [ "$1" = "uninstall" ]; then
    echo "Uninstalling your hosts file and restoring initial hosts file..."
    if [ -f "$INITIAL_HOSTS" ]; then
    @@ -105,6 +110,10 @@ 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
    echo "Installing hosts list..."
    cp -v $NEW_HOSTS $HOSTS_PATH

  10. @Eliastik Eliastik revised this gist Apr 16, 2018. 1 changed file with 38 additions and 8 deletions.
    46 changes: 38 additions & 8 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -4,20 +4,22 @@
    #
    # Author: George Lesica <george@lesica.com>
    # Enhanced by Eliastik ( eliastiksofts.com/contact )
    # Version 1.1 (16 april 2018)
    #
    # 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.
    # 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, 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" )
    # 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=( "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"
    @@ -35,17 +37,45 @@ if ! [ -x "$(command -v curl)" ]; then
    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
    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
    if [ -f "$INITIAL_HOSTS" ]; then
    cat $INITIAL_HOSTS>$NEW_HOSTS
    else
    echo "The initial host file doesn't exist: $INITIAL_HOSTS"
    echo "The initial hosts file doesn't exist: $INITIAL_HOSTS"
    echo "">$NEW_HOSTS
    fi

    @@ -66,7 +96,7 @@ do
    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..."
    echo "Download failed $NB_MAX_DOWNLOAD_RETRYING time(s). Check your Internet connection and the hosts source then try again. Exiting..."
    exit 1;
    fi
    done
  11. @Eliastik Eliastik revised this gist Dec 29, 2017. 1 changed file with 5 additions and 4 deletions.
    9 changes: 5 additions & 4 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -21,10 +21,11 @@ HOSTS_URLS=( "http://someonewhocares.org/hosts/zero/hosts/" "https://pgl.yoyo.or
    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" 1>&2
    echo "This script must be run as root. Exiting..." 1>&2
    exit 1
    fi

    @@ -64,9 +65,9 @@ do
    curl -s --fail "$i">>$NEW_HOSTS && break ||
    nberror=$((nberror + 1))
    echo "Download failed ! Retrying..."
    if [ $nberror -ge 5 ]; then
    echo "Download failed 5 times. Check your Internet connection and the host source then try again. Continue..."
    break
    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
  12. @Eliastik Eliastik revised this gist Dec 29, 2017. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -9,7 +9,7 @@
    # 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 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.
    #
    @@ -48,6 +48,12 @@ else
    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
  13. @Eliastik Eliastik revised this gist Dec 29, 2017. No changes.
  14. @Eliastik Eliastik revised this gist Dec 29, 2017. 1 changed file with 58 additions and 8 deletions.
    66 changes: 58 additions & 8 deletions update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,24 @@
    #!/usr/bin/env sh
    #!/bin/bash

    # Filename: update-hosts.sh
    #
    # Author: George Lesica <george@lesica.com>
    # Description: Replaces the HOSTS file with a customized version that blocks
    # domains that serve ads and malicious software, creating a backup of the old
    # file.
    # 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.

    HOSTS_URL="http://someonewhocares.org/hosts/zero/hosts"
    # 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"

    @@ -16,12 +28,50 @@ if [ "$(id -u)" -ne "0" ]; then
    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

    # Grab hosts file
    wget -O $NEW_HOSTS $HOSTS_URL
    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 5 ]; then
    echo "Download failed 5 times. Check your Internet connection and the host source then try again. Continue..."
    break
    fi
    done
    done

    # Backup old hosts file
    cp -v $HOSTS_PATH ${HOSTS_PATH}.bak$(date -u +%s)
    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
    rm $NEW_HOSTS*
    echo "Removing cache..."
    rm $NEW_HOSTS*
    echo "Done !"
  15. @glesica glesica revised this gist Apr 8, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion update-hosts.sh
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/usr/bin/env sh

    # Filname: update-hosts.sh
    # Filename: update-hosts.sh
    # Author: George Lesica <george@lesica.com>
    # Description: Replaces the HOSTS file with a customized version that blocks
    # domains that serve ads and malicious software, creating a backup of the old
  16. @glesica glesica renamed this gist Apr 8, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  17. @glesica glesica created this gist Apr 8, 2013.
    27 changes: 27 additions & 0 deletions update.hosts.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    #!/usr/bin/env sh

    # Filname: update-hosts.sh
    # Author: George Lesica <george@lesica.com>
    # Description: Replaces the HOSTS file with a customized version that blocks
    # domains that serve ads and malicious software, creating a backup of the old
    # file.

    HOSTS_URL="http://someonewhocares.org/hosts/zero/hosts"
    NEW_HOSTS="hosts"
    HOSTS_PATH="/etc/hosts"

    # Check for root
    if [ "$(id -u)" -ne "0" ]; then
    echo "This script must be run as root" 1>&2
    exit 1
    fi

    # Grab hosts file
    wget -O $NEW_HOSTS $HOSTS_URL

    # Backup old hosts file
    cp -v $HOSTS_PATH ${HOSTS_PATH}.bak$(date -u +%s)
    cp -v $NEW_HOSTS $HOSTS_PATH

    # Clean up old downloads
    rm $NEW_HOSTS*