Skip to content

Instantly share code, notes, and snippets.

@aldodelgado
Forked from kolpanic/update_hosts
Created May 28, 2019 01:27
Show Gist options
  • Select an option

  • Save aldodelgado/05dbdce0ce03466642c611fd910199a7 to your computer and use it in GitHub Desktop.

Select an option

Save aldodelgado/05dbdce0ce03466642c611fd910199a7 to your computer and use it in GitHub Desktop.
Uses the MVPS hosts file to block unwanted parasites by redirecting them to `0.0.0.0`
#!/bin/bash
# Downloads the MVPS hosts file, and append it to the system hosts file
#
# If you want to make any manual persistent changes to the system hosts
# file, run this script first, then make sure that the change is backed up to
# /etc/hosts.orig.
if [ "$(whoami)" != "root" ]; then
echo "Sorry, this script must be run as root."
exit 1
fi
# Back up the original hosts file
if [ ! -f /etc/hosts.orig ]; then
cp /etc/hosts /etc/hosts.orig
fi
# Create tmp dir
if [ ! -d /tmp/update_hosts/ ]; then
mkdir /tmp/update_hosts/
fi
# Download the MVPS hosts file
curl http://winhelp2002.mvps.org/hosts.txt > /tmp/update_hosts/hosts.mvps1
# Convert the file to UTF-8 and the DOS line endings to UNIX
iconv -t UTF8//IGNORE -f ASCII /tmp/update_hosts/hosts.mvps1 > /tmp/update_hosts/hosts.mvps2
tr -d '\r' < /tmp/update_hosts/hosts.mvps2 > /tmp/update_hosts/hosts.mvps3
# Remove stuff at the top
sed -n '/Start of entries generated by MVPS HOSTS]/,$p' /tmp/update_hosts/hosts.mvps3 > /tmp/update_hosts/hosts.mvps4
# Append to the original hosts file
cat /etc/hosts.orig /tmp/update_hosts/hosts.mvps4 > /etc/hosts
# Clean up
rm -rf /tmp/update_hosts/
# Flush DNS
killall -HUP mDNSResponder
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment