Announcement

Collapse
No announcement yet.

NEW - Script to automate host files to block bad domains

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    NEW - Script to automate host files to block bad domains

    I needed to update my hosts file and decide to automate the process so I don't have to keep doing it. If anyone knows has a better way to do this, please chime in.

    This script uses the "Ultimate-Hosts-Blacklist" hosts files from here and a script of my own making, along with a file I call "hosts.local". The default hosts file from Ultimate-Hosts-Blacklist has a generic entry for /etc/hosts that doesn't have your hostname in it or (obviously) any other custom entries you might have added. I have all 8 of my local systems defined in my hosts file to ease remote terminal access to them.

    I created a folder called "/etc/hosts.d/" on my system, but you can use anywhere you wish including a hidden folder in your home.

    I copied my custom hosts file as /etc/hosts.d/hosts.local, then the script does the rest:

    Code:
    #!/bin/bash
    
    # Check to see if URL is responding and fail if not leaving record of failure in /etc/hosts.d/
    ONLINE=`curl -Is https://hosts.ubuntu101.co.za |head -1 | awk '{print $3}'`
    
    if [[ $ONLINE != 'OK' ]]; then
            echo 'Download failed' > /etc/hosts.d/failed.$(date +%y%m%d-%H%M%S)
            exit 1
    fi​
    
    # Move old hosts file into hosts.d as a dated backup
    mv /etc/hosts /etc/hosts.d/hosts.$(date +%y%m%d-%H%M%S)
    
    # Download the latest Ultimate-Hosts-Blacklist as /etc/hosts.d/block
    wget https://hosts.ubuntu101.co.za/hosts -O /etc/hosts.d/block
    
    # Download and install the latest hosts.deny from Ultimate-Hosts-Blacklist
    wget https://hosts.ubuntu101.co.za/superhosts.deny -O /etc/hosts.deny
    
    # Set variable to locate where the blocked domains begin and the generic hosts definitions end
    START=`grep -n '# START HOSTS LIST' /etc/hosts.d/block  | cut -d : -f 1`
    START=$(($START + 1))
    
    # Copy my hosts.local as the new /etc/hosts file
    cp /etc/hosts.d/hosts.local /etc/hosts
    
    # Trim the generic hosts beginning from Ultimate-Hosts-Blacklist and append it to the new hosts file
    tail +$START /etc/hosts.d/block >> /etc/hosts
    
    # Exit the script
    exit 0​
    The only "fail-safe" is if the server is down. If this happens, it leaves a file behind documenting the failed download and leave the previous hosts file in place.l

    I need to add a way to "clean up" old hosts file backups and failed noticies.

    Currently I have this running as a weekly cron job.
    Last edited by oshunluvr; Jul 01, 2024, 08:52 AM.

    Please Read Me

    #2
    Funny note: The domain is downright now, lol

    Please Read Me

    Comment


      #3
      I've been using stevenblack's host file for several years. One side effect (?) is that it speeds up surfing considerable since all the ad server and pixel links get sent to the infinite bit bucket in the sky. When you browse pages the locations were ads would have appeared are usually white blocks. I also block Google's stuff, which sometimes breaks links embedded in urls that have google tracking hexidecimal strings in them.
      "A nation that is afraid to let its people judge the truth and falsehood in an open market is a nation that is afraid of its people.”
      – John F. Kennedy, February 26, 1962.

      Comment

      Working...
      X