dig +short的行为是当运行一个不返回任何结果的查询(与超时无关,只是一个空的答案)时不返回空(null)。
当自己运行查询时,这是可以的,但是当与-f参数结合并运行一批查询时,它是不好的!
dig +short -f queries.txt
queries.txt: A somedomain.com TXT otherdomain.com A somedomain.com
现在,如果DNS服务器没有为TXT otherdomain.com(即ANSWER:0)返回任何内容,而不是超时或其他内容,那么上述dig命令的输出将如下所示:
dig +short -f queries.txt 1.2.3.4 1.2.3.4
即只有2行。 对“粘贴”和其他类似的命令不好。 你不能再合并queries.txt的输出和dig的输出。
有什么优雅的可以在这里完成?
在这种情况下,没有真正的方法可以做出自己想做的事情。 在使用批量数据时,这只是工作的错误工具。
我遇到这个问题时find的解决scheme是使用filter的组合: +noall +question +answer 。 +noallclosures所有显示字段, +question显示正在进行的查询; 评论前缀, +answer显示答案。
输出如下所示:
$ 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
如果你没有回复,你会看到两个相邻的问题。 您不会知道查询失败的原因 ,因为此输出不显示RCODE(也不是+short ),但输出足以分析批量数据集并find需要更详细分析的logging。
如果您发现自己正在对DNS引荐进行批量分析,请切换+authority 。
我认为这个Python脚本将工作。
from subprocess import Popen, PIPE def dig(z): proc = Popen( args='dig +short {} | head -n 1'.format(z), shell=True, stdout=PIPE ) return proc.communicate()[0] result = dict() with open('queries.txt') as zones: for zone in zones.readlines(): zone = zone.rstrip() result[zone] = dig(zone) with open('queries.txt', 'w+') as results: for key, value in result.items(): if value == '': value = 'no answer\n' results.write('{} : {}'.format(key, value))