forked from lennylxx/ipv6-hosts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate_hosts.sh
executable file
·81 lines (63 loc) · 1.38 KB
/
update_hosts.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
76
77
78
79
80
81
#!/bin/bash
hosts_file=$1
new_hosts_file=$2
he_net="2001:470:20::2"
google_a="2001:4860:4860::8888"
google_b="2001:4860:4860::8844"
dns=$he_net
blackhole=(
'10::2222'
'101::1234'
'21:2::2'
'2001::212'
'2001:da8:112::21ae'
'2003:ff:1:2:3:4:5fff:6'
'2003:ff:1:2:3:4:5fff:7'
'2003:ff:1:2:3:4:5fff:8'
'2003:ff:1:2:3:4:5fff:9'
'2003:ff:1:2:3:4:5fff:10'
'2003:ff:1:2:3:4:5fff:11'
'2003:ff:1:2:3:4:5fff:12'
'2123::3e12')
num=1
while read line
do
{
#delete CR
line=$(printf "$line"|tr -d '\r')
#printf "$line"|od -tx1
if [[ $line == "" ]]; then
printf "\r\n" >> $new_hosts_file
continue
fi
if [ "${line:0:2}" == "##" ]; then
printf "$line\r\n" >> $new_hosts_file
continue
fi
if [ "${line:0:1}" == "#" ]; then
line=${line#'#'}
fi
url=$(printf "$line"|cut -d" " -f2)
result=$(nslookup -querytype=AAAA "$url" "$dns"|grep 'AAAA address')
name=$(printf "$result"|cut -f1)
ip=$(printf "$result"|cut -d' ' -f4)
for var in "${blackhole[@]}"; do
if [[ $ip == "$var" && $ip != "" ]]; then
ip=$(nslookup -vc -querytype=AAAA "$url" "$dns"|grep 'AAAA address'|cut -d' ' -f4)
break
fi
done
if [[ $ip == "" ]]; then
printf "#$line\r\n" >> $new_hosts_file
continue
fi
if [[ $name != $url && $name != "" ]]; then
url=${url}" #"${name}
fi
printf "$ip $url\r\n" >> $new_hosts_file
#print log to stdio
echo "$num" "$ip" "$url"
num=$((num+1))
}
done < $hosts_file
exit 0