-
Notifications
You must be signed in to change notification settings - Fork 0
/
updatehosts.sh
53 lines (38 loc) · 1.8 KB
/
updatehosts.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/bash
set -o nounset
set -o errexit
###############################################################################
##### This script will generate an IPv4-only hosts file that blocks domains
##### responsible for advertisements, analytics, and malicious activities
#####
##### Modified from: https://github.com/fr0stycl34r/update-hosts
###############################################################################
## We must be root in order to modify the contents of /etc
rootcheck() {
if [[ $UID -ne 0 ]]; then
echo "Please run this script as root"
exit 1
fi
}
rootcheck
## Backup the current hosts file
mv /etc/hosts /etc/hosts.bak
## Create a temporary file to dump the various lists into
temphosts=$(mktemp)
## Download various pre-made lists into our temp file
wget -nv -O - http://someonewhocares.org/hosts/hosts >> $temphosts
wget -nv -O - http://winhelp2002.mvps.org/hosts.txt >> $temphosts
wget -nv -O - http://www.malwaredomainlist.com/hostslist/hosts.txt >> $temphosts
## wget -nv -O - http://adblock.gjtech.net/?format=hostfile >> $temphosts
wget -nv -O - http://hosts-file.net/ad_servers.txt >> $temphosts
wget -nv -O - "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&showintro=0&mimetype=plaintext" >> $temphosts
wget -nv -O - http://sysctl.org/cameleon/hosts.win >> $temphosts
wget -nv -O - http://adaway.org/hosts.txt >> $temphosts
## Cleanup the list; remove commented lines, remove duplicates, etc
sed -e 's/\r//' -e '/^127.0.0.1/!d' -e '/localhost/d' -e 's/0.0.0.0/127.0.0.1/' -e 's/ \+/\t/' -e 's/#.*$//' -e 's/[ \t]*$//' < $temphosts | sort -u > /etc/hosts
## Append some necessary stuff to the beginning of the file
echo "# Last updated on $(date)
127.0.0.1 localhost
127.0.0.1 $HOSTNAME" | cat - /etc/hosts >> temp && mv temp /etc/hosts
## A bit of cleanup
rm $temphosts