forked from dibdot/DoH-IP-blocklists
-
Notifications
You must be signed in to change notification settings - Fork 0
/
doh-lookup.sh
executable file
·78 lines (73 loc) · 2.52 KB
/
doh-lookup.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
#!/bin/sh
# doh-lookup - retrieve IPv4/IPv6 addresses via nslookup from given domain & resolver list
# and write the adjusted output to separate lists (IPv4/IPv6 addresses plus domains)
# Copyright (c) 2019-2022 Dirk Brenken ([email protected])
#
# This is free software, licensed under the GNU General Public License v3.
#
# prepare environment
#
export LC_ALL=C
: >./ipv4.tmp
: >./ipv6.tmp
: >./domains.tmp
: >./domains_abandoned.tmp
input="./doh-domains_overall.txt"
# set the dns utility, 'host' or 'nslookup'
#
dns_tool="host"
dns_tool="$(command -v ${dns_tool})"
# set upstream dns server
#
upstream="1.1.1.1 8.8.8.8 9.9.9.9 208.67.222.222"
# domain per resolver processing
#
while IFS= read -r domain; do
domain_ok="false"
for resolver in ${upstream}; do
out="$(
"${dns_tool}" "${domain}" "${resolver}" 2>/dev/null
printf "%s" "${?}"
)"
if [ "$(printf "%s" "${out}" | tail -1)" = "0" ]; then
if [ "${dns_tool##*/}" = "host" ]; then
ips="$(printf "%s" "${out}" | awk '{if ($0 ~ "has address|has IPv6 address"){ORS=" ";print $NF}}')"
elif [ "${dns_tool##*/}" = "nslookup" ]; then
ips="$(printf "%s" "${out}" | awk '/^Address[ 0-9]*: /{ORS=" ";print $NF}')"
fi
if [ -n "${ips}" ]; then
printf "%-40s%-22s%s\n" "OK : ${domain}" "DNS: ${resolver}" "IP: ${ips}"
for ip in ${ips}; do
if [ "${ip}" = "0.0.0.0" ] || [ "${ip}" = "::" ]; then
continue
else
domain_ok="true"
if [ -n "$(printf "%s" "${ip}" | awk '/^(([0-9]{1,3}\.){3}[0-9]{1,3}(\/[0-9]{1,2})?)([[:space:]]|$)/{print $1}')" ]; then
printf "%-20s%s\n" "${ip}" "# ${domain}" >>./ipv4.tmp
else
printf "%-40s%s\n" "${ip}" "# ${domain}" >>./ipv6.tmp
fi
fi
done
else
out="$(printf "%s" "${out}" | grep -o "connection timed out\|SERVFAIL\|NXDOMAIN")"
printf "%-40s%-22s%s\n" "ERR: ${domain}" "DNS: ${resolver}" "RC: ${out:-"unknown"}"
fi
else
out="$(printf "%s" "${out}" | grep -o "connection timed out\|SERVFAIL\|NXDOMAIN")"
printf "%-40s%-22s%s\n" "ERR: ${domain}" "DNS: ${resolver}" "RC: ${out:-"unknown"}"
fi
done
if [ "${domain_ok}" = "false" ]; then
printf "%s\n" "${domain}" >>./domains_abandoned.tmp
else
printf "%s\n" "${domain}" >>./domains.tmp
fi
done <"${input}"
# final sort/merge step
#
sort -b -u -n -t. -k1,1 -k2,2 -k3,3 -k4,4 ./ipv4.tmp >./doh-ipv4.txt
sort -b -u -k1,1 ./ipv6.tmp >./doh-ipv6.txt
sort -b -u ./domains.tmp >./doh-domains.txt
sort -b -u ./domains_abandoned.tmp >./doh-domains_abandoned.txt
rm ./ipv4.tmp ./ipv6.tmp ./domains.tmp ./domains_abandoned.tmp