-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdig.txt
226 lines (177 loc) · 6.93 KB
/
dig.txt
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
##step: define a function to get_ipaddr and $collect_nhf_ip
function get_ipaddr {
local hostname=$1
local query_type=A
ip_address=""
# use host command for DNS lookup operations
host -t ${query_type} "${hostname}" &>/dev/null
if [ "$?" -eq "0" ]; then
# get ip address
# if The domain have multiple ip address. merge it at one line
ip_address="$(host -t ${query_type} "${hostname}" | awk '/has.*address/{print $NF}' | sort -rn | awk BEGIN{RS=EOF}'{gsub(/\n/," ");print}')"
else
echo "############"
fi
# display ip
echo "$ip_address"
}
➜ shell dig www.baidu.com. +short +noall +answer
www.a.shifen.com.
14.215.177.39
14.215.177.38
➜ shell host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.38
www.a.shifen.com has address 14.215.177.39
host -t A www.baidu.com|awk '/has.*address/{print $NF}' | sort -rn | awk BEGIN{RS=EOF}'{gsub(/\n/,",");print}'
14.215.177.39,14.215.177.38
DUCKDNS=( "sub1.duckdns.org"
"sub2.duckdns.org"
"sub3.duckdns.org"
"sub4.duckdns.org" )
function resolveIP () {
CURRENT_IP=$(dig +short $1)
echo $CURRENT_IP
}
for HOST in "${DUCKDNS[@]}"
do
IP=$(resolveIP $HOST)
echo -e "$HOST ... $IP"
done
➜ ~ dig www.baidu.com
; <<>> DiG 9.16.24 <<>> www.baidu.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 42729
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 512
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 456 IN CNAME www.a.shifen.com.
www.a.shifen.com. 86 IN A 14.215.177.39
www.a.shifen.com. 86 IN A 14.215.177.38
;; Query time: 39 msec
;; SERVER: 114.114.114.114#53(114.114.114.114)
;; WHEN: Sun Mar 20 18:19:40 CST 2022
;; MSG SIZE rcvd: 101
➜ ~ dig @119.29.29.29 www.baidu.com
; <<>> DiG 9.16.24 <<>> @119.29.29.29 www.baidu.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 8641
;; flags: qr rd ra; QUERY: 1, ANSWER: 3, AUTHORITY: 0, ADDITIONAL: 1
;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 4096
; COOKIE: 69dd9c40deedb114 (echoed)
;; QUESTION SECTION:
;www.baidu.com. IN A
;; ANSWER SECTION:
www.baidu.com. 909 IN CNAME www.a.shifen.com.
www.a.shifen.com. 53 IN A 14.215.177.39
www.a.shifen.com. 53 IN A 14.215.177.38
;; Query time: 36 msec
;; SERVER: 119.29.29.29#53(119.29.29.29)
;; WHEN: Sun Mar 20 18:19:54 CST 2022
;; MSG SIZE rcvd: 113
➜ ~ host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.39
www.a.shifen.com has address 14.215.177.38
➜ ~ dig @119.29.29.29 AAAA +short
l.root-servers.net.
m.root-servers.net.
a.root-servers.net.
b.root-servers.net.
c.root-servers.net.
d.root-servers.net.
e.root-servers.net.
f.root-servers.net.
g.root-servers.net.
h.root-servers.net.
i.root-servers.net.
j.root-servers.net.
k.root-servers.net.
➜ ~ dig @119.29.29.29 www.baidu.com AAAA +short
www.a.shifen.com.
Get a short answer?
When all you want is a quick answer, the +short option is your friend:
➜ ~ dig @119.29.29.29 www.baidu.com +short
www.a.shifen.com.
14.215.177.38
14.215.177.39
Get a not-quite-so-short answer
➜ ~ dig +nocmd www.baidu.com A +noall +answer
www.baidu.com. 131 IN CNAME www.a.shifen.com.
www.a.shifen.com. 60 IN A 14.215.177.38
www.a.shifen.com. 60 IN A 14.215.177.39
Get a long answer?
dig +nocmd www.baidu.com any +multiline +noall +answer
www.baidu.com. 110 IN CNAME www.a.shifen.com.
进行批量查找吗?如果要查找大量主机名,可以将它们放入一个文件(每行一个名称),然后使用-f选项依次查询每个主机名对多个主机名执行完整查找dig-f/path/to/host list。txt#相同,输出更加集中-f/path/to/host list。txt+noall+answer
批量查找
Do bulk lookups? Bulk Queries
X
If you want to look up a large number of hostnames, you can put them in a file (one name per line) and use the -f option to query each one in turn.
# do full lookups for a number of hostnamesdig -f /path/to/host-list.txt# the same, with more focused output
dig -f /path/to/host-list.txt +noall +answer
➜ ~ cat /tmp/b.txt
www.baidu.com
www.sohu.com
www.github.com
www.csdn.net
➜ ~ dig -f /tmp/b.txt +noall +answer
www.baidu.com. 988 IN CNAME www.a.shifen.com.
www.a.shifen.com. 207 IN A 14.215.177.38
www.a.shifen.com. 207 IN A 14.215.177.39
www.sohu.com. 1389 IN CNAME gs.a.sohu.com.
gs.a.sohu.com. 77 IN CNAME fwh.a.sohu.com.
fwh.a.sohu.com. 37 IN A 119.96.200.204
www.github.com. 3503 IN CNAME github.com.
github.com. 37 IN A 20.205.243.166
www.csdn.net. 37 IN A 39.106.226.142
dig -f /tmp/b.txt +noall +answer |awk -F" " '{if($4~/^A/) print$0}'
www.a.shifen.com. 118 IN A 14.215.177.39
www.a.shifen.com. 118 IN A 14.215.177.38
fwh.a.sohu.com. 89 IN A 119.96.200.204
github.com. 37 IN A 20.205.243.166
www.csdn.net. 37 IN A 39.106.226.142
➜ ~ dig +nocmd www.baidu.com +noall +answer
www.baidu.com. 1002 IN CNAME www.a.shifen.com.
www.a.shifen.com. 52 IN A 14.215.177.38
www.a.shifen.com. 52 IN A 14.215.177.39
➜ ~ dig +nocmd www.baidu.com +noall +answer +short
www.a.shifen.com.
14.215.177.38
14.215.177.39
dig -f /tmp/b.txt +noall +answer |awk -F" " '{if($4~/^A/) print$0}'|awk '{S[$1]=$5","S[$1]}END{for (a in S) print a, S[a]}'|sed 's/,$//g'
+noall +answer +nocmd
noall:不输出任何内容
answer:只对结果输出
我既想拿到域名又拿到结果那么用+noall +answer来过滤
➜ ~ dig -f /tmp/b.txt +noall +answer |awk -F" " '{if($4~/^A/) print$0}'|sort -k1|awk '{S[$1]=$5","S[$1]}END{for (a in S) print a, S[a]}'|sed 's/,$//g'
www.a.shifen.com. 14.215.177.39,14.215.177.38
github.com. 20.205.243.166
fwh.a.sohu.com. 119.96.200.204
www.csdn.net. 39.106.226.142
假如第四个字短为A记录 然后打印第一个字段和第五个字段 用空格分割 然后排序 去重复行
然后awk '{S[$1]=$2","S[$1]}END{for (a in S) print a, S[a]} 合并第二个字段,假如第一个字段相当的情况下,
再去掉尾椎的,
dig -f /tmp/b.txt +noall +answer |awk -F" " '{if($4~/^A/) print$1" "$5}'|sort -k1|uniq|
这种办法也不好,比如就取不到我原来的域名了 比如www.baidu.com
host www.baidu.com
www.baidu.com is an alias for www.a.shifen.com.
www.a.shifen.com has address 14.215.177.39
www.a.shifen.com has address 14.215.177.3
我期待的结果是别名之后的2个IP address
dig -f /tmp/b.txt +noall +answer |awk -F" " '{if($4~/^A/) print$1" "$5}'|sort -k1|uniq|awk '{S[$1]=$2","S[$1]}END{for (a in S) print a, S[a]}'|sed 's/,$//g'
To get a list of all the address(es) for a domain name, use the a option:
dig +nocmd google.com a +noall +answer
Querying CNAME records
To find the alias domain name use the cname option:
dig +nocmd mail.google.com cname +noall +answer
https://linuxize.com/post/how-to-use-dig-command-to-query-dns-in-linux/
https://blog.51cto.com/maorui2005/1741029
https://phoenixnap.com/kb/linux-dig-command-examples