-
Notifications
You must be signed in to change notification settings - Fork 23
/
updateHosts.sh
executable file
·75 lines (69 loc) · 3.19 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/bin/bash
# Last revision: 2016/01/04
# Script originally belongs to SteveRiley, thanks for the work.
# I just did some modifications, bug fixing etc.
# Let's create our temporary files
temphosts1=$(mktemp)
temphosts2=$(mktemp)
# Obtain various hosts files and merge into one
# You can add/remove more as you wish.
echo "Downloading ad-blocking hosts files..."
curl -ksS http://winhelp2002.mvps.org/hosts.txt > $temphosts1
curl -ksS http://someonewhocares.org/hosts/zero/hosts >> $temphosts1
curl -ksS "http://pgl.yoyo.org/adservers/serverlist.php?hostformat=hosts&mimetype=plaintext&useip=0.0.0.0" >> $temphosts1
curl -ksS https://raw.githubusercontent.com/nomadturk/vpn-adblock/master/hosts-sources/Turkish/hosts >> $temphosts1
curl -ksS https://adaway.org/hosts.txt >> $temphosts1
curl -ksS http://www.malwaredomainlist.com/hostslist/hosts.txt >> $temphosts1
curl -ksS http://hosts-file.net/ad_servers.txt >> $temphosts1
curl -ksS https://raw.githubusercontent.com/nomadturk/vpn-adblock/master/hosts-sources/nomad/hosts >> $temphosts1
# Do some work on the file:
# 1. Remove MS-DOS carriage returns
# 2. Delete all lines that don't begin with 127.0.0.1 or 0.0.0.0
# 3. Delete any lines containing the word localhost because we'll obtain that from the original hosts file
# 4. Replace 127.0.0.1 with 0.0.0.0 because then we don't have to wait for the resolver to fail
# 5. Scrunch extraneous spaces separating address from name into a single tab
# 6. Delete any comments on lines
# 7. Clean up leftover trailing blanks
# 8. Finally, delete all lines that don't begin with 0.0.0.0 to make sure that all remnants are removed
# Pass all this through sort with the unique flag to remove duplicates and save the result
echo
echo "Parsing"
echo "Cleaning"
echo "De-duplicating"
echo "Sorting..."
echo
sed -e 's/\r//' \
-e '/^127.0.0.1\|0.0.0.0/!d' \
-e '/localhost/d' \
-e 's/127.0.0.1/0.0.0.0/' \
-e 's/ \+/\t/' \
-e 's/#.*$//' \
-e 's/[ \t]*$//' \
-e '/^0.0.0.0/!d' \
< $temphosts1 | sort -u > $temphosts2
# Remove 0.0.0.0 accidentally referring to itself
sed -i 's/^0.0.0.0\\t0.0.0.0/0.0.0.0/' $temphosts2
# Combine system hosts with adblocks
echo "Creating supplementary hosts file is complete..."
echo -e "#<local-data>" > ~/hosts.supp
echo -e "\n# Ad blocking hosts generated "$(date) >> ~/hosts.supp
cat $temphosts2 > ~/hosts.supp.tmp
totallines="$(grep --regexp="$" --count ~/hosts.supp.tmp)"
echo -e "# Total number of blocked hosts are:" $totallines >> ~/hosts.supp
rm -rf ~/hosts.supp.tmp
cat $temphosts2 >> ~/hosts.supp
echo -e "#</local-data>" >> ~/hosts.supp
# Clean up temp files and remind user to copy new file
echo "Time to clean up..."
rm $temphosts1 $temphosts2
echo "Done."
echo
echo "Copying ad-blocking hosts file with this command:"
echo "We are going to use a supplementary file named hosts.supp"
echo "This is required to not to mess with the existing hosts file"
echo "cp ~/hosts.supp /etc"
cat ~/hosts.supp > /etc/hosts.supp
echo
echo "You can always delete or update this file or just remove any references"
echo "from DNSMasq or whatever you use it with"
echo