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:
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.
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
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.
Comment