-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmakehosts
More file actions
executable file
·69 lines (69 loc) · 2.59 KB
/
makehosts
File metadata and controls
executable file
·69 lines (69 loc) · 2.59 KB
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
#!/bin/bash
# Read from a list of domain names, and use dig to create a hosts file that can be used
# instead of DNS.
domains="${domains-domains}"
blacklist="${blacklist-blacklist}"
badlist="${badlist-badlist}"
if ! ls domains >/dev/null 2>/dev/null; then
echo "You need to make a list of domain names (websites, like google.com), "
echo "and put it in a file called \"domains\" (not domains.txt) in the "
echo "working directory (currently $(pwd)/). "
echo "You can also run the addomains script to add as many domains as you want,"
echo "and then optimize it with the optdomains script."
echo "When you have a domains file, run this script again."
echo "If you absolutely need to run makehosts against a file other than 'domains',"
echo "you can run: domains=/path/to/my/domainsfile $0"
exit 1
fi
mkdir -p old
mv hosts old/hosts.backedup$(date +%y%m%d%s) 2>/dev/null
echo "# This hosts file generated by makehosts (by george) on $(date | sed 's/\ \ /\ 0/')." > hosts
echo -n Generating hosts file. . .\
dom=0
ips=0
dtim=0
stim=$(date +%s)
lnum=$(wc -l $domains | gawk '{print $1}')
wnum=$(wc -w $domains | gawk '{print $1}')
if [ "$lnum" != "$wnum" ]; then
echo "Please run the optdomains script, and make sure you have only"
echo "one domain on each line of the 'domains' file."
exit 1
fi
while read domain; do
if [ -n "$1" -a "$1" == "-v" ]; then
echo -n "$domain "
verbose=yes
fi
grep -iq "^$domain$" "$blacklist" && continue # skip domains on the blacklist
grep -iq "^$domain$" "$badlist" && (echo "127.0.0.1 $domain www.$domain" >> hosts) && continue # malware/bad domains
list=$(dig +short $domain)
for ipaddr in $list; do
if (echo $ipaddr | grep -e '^[0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+$' >/dev/null); then
echo $ipaddr $domain www.$domain >> hosts
let ips=$ips+1
else
if [ -n "$verbose" ]; then
echo -n +\ #this is not an error, it just shows some extra info.
fi
fi
done
if [ $lnum -ge 250 -a $dtim -eq 0 ]; then
dtim=1
elif [ $lnum -lt 250 ]; then
echo -n .\
else
echo -n .\
dtim=0
fi
let dom=$dom+1
done < $domains
etim=$(date +%s)
let tim=$etim-$stim
echo -n "# $dom domains resolved to $ips IPs in about $tim seconds. Finished $(date | sed 's/\ \ /\ 0/')." >> hosts
echo done.
echo "Tadah! All done. " >&2
echo "$dom domains resolved to $ips IPs in about $tim seconds." >&2
# To use this script, make a list of domain names in a file called 'domains'. Put
# one domain name on each line of the file, and do not include www.blah domains,
# as www.'s will be automatically added for each domain on the list.