大网数据广告活动
服务器运维问答

How to get dig +short to return something when there is no answer?

· 52CE 我爱测

The behavior of dig +short is to return nothing (null) when running a query that returns no answer (nothing to do with the timeout, just a definite null answer). It is ok when running a query by itself, but when combining with an…

问题原文

原帖链接 · Alex · CC BY-SA 4.0

The behavior of dig +short is to return nothing (null) when running a query that returns no answer (nothing to do with the timeout, just a definite null answer).

It is ok when running a query by itself, but when combining with an -f parameter and running a batch of queries, it is terrible!

dig +short -f queries.txt

queries.txt: A somedomain.com TXT otherdomain.com A somedomain.com

Now, if the DNS server returns nothing for TXT otherdomain.com (i.e., ANSWER: 0), not timeout or something else, then the output of the above dig command will be something like:

dig +short -f queries.txt 1.2.3.4 1.2.3.4

i.e., only two lines. Not suitable for "paste" and other similar commands. You can no longer merge the output of queries.txt and the production output of dig.

Anything elegant can be done here?

已采纳解答

原帖链接 · Andrew B · CC BY-SA 3.0

There is no real way to make +short do what you want it to in this context. It's simply the wrong tool for the job when working with bulk data.

The solution I found when running into this problem was to use a combination of filters: +noall +question +answer. +noall turns all display fields off, +question displays the query being made with a ; comment prefix, and +answer displays the answer.

The output looks like this:

$ dig +noall +question +answer google.com serverfault.com
;google.com.                    IN      A
google.com.             284     IN      A       74.125.137.101
google.com.             284     IN      A       74.125.137.138
google.com.             284     IN      A       74.125.137.102
google.com.             284     IN      A       74.125.137.100
google.com.             284     IN      A       74.125.137.113
google.com.             284     IN      A       74.125.137.139
;serverfault.com.               IN      A
serverfault.com.        187     IN      A       198.252.206.16

In the event that you get no response back, you will see two adjacent questions. You won't know why the query failed as this output doesn't display a RCODE (neither does +short), but the output is sufficient for analyzing a bulk data set and locating records that need more verbose analysis.

If you find yourself doing bulk analysis of DNS referrals, switch +answer out for +authority.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2014-08-12)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0 / CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。