Skip to content

Instantly share code, notes, and snippets.

@renesansz
Last active December 6, 2023 22:48
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 renesansz/08bdff96322d2043fa3b4c44f97d217b to your computer and use it in GitHub Desktop.
Save renesansz/08bdff96322d2043fa3b4c44f97d217b to your computer and use it in GitHub Desktop.
A shell script that updates your /etc/hosts file with adblock, malware blocking, and malicious URLs blocking based on https://github.com/StevenBlack/hosts.
#!/bin/bash
# Set the URLs for the hosts files
HOSTS_URLS=(
"https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts"
"https://raw.githubusercontent.com/StevenBlack/hosts/master/alternates/fakenews-gambling-only/hosts"
"https://raw.githubusercontent.com/badmojr/1Hosts/master/Lite/hosts.txt"
"https://tgc.cloud/downloads/hosts.txt"
"https://raw.githubusercontent.com/d3ward/toolz/master/src/d3host.txt"
# Add more URLs as needed
)
# Set the path to the hosts file on MacOS
HOSTS_FILE="/etc/hosts"
# Create a timestamp for the backup filename
TIMESTAMP=$(date "+%Y_%m_%d_%H-%M-%S")
BACKUP_FILE="/Users/renesansz/backups/etc/hosts.backup_$TIMESTAMP"
# Create a temporary hosts file
TEMP_HOSTS_FILE="/tmp/temp_hosts"
# Backup the existing hosts file
sudo cp "$HOSTS_FILE" "$BACKUP_FILE"
# Loop through each URL and append the content to the temporary hosts file
for URL in "${HOSTS_URLS[@]}"; do
# Download the hosts file and append to the temporary file
sudo curl -o - "$URL" >> "$TEMP_HOSTS_FILE"
done
# Remove lines starting with '#' and whitespace
sudo sed -i '' '/^#/d' "$TEMP_HOSTS_FILE"
sudo sed -i '/^0\.0\.0\.0 0\.0\.0\.0$/d' "$TEMP_HOSTS_FILE"
# Replace the existing hosts file with the temporary hosts file
sudo mv "$TEMP_HOSTS_FILE" "$HOSTS_FILE"
echo "Hosts file updated successfully."
sudo dscacheutil -flushcache;sudo killall -HUP mDNSResponder
echo "DNS cache flushed."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment