python 如何检查主机的DNS TXT记录的值?

ndh0cuux  于 2022-11-21  发布在  Python
关注(0)|答案(5)|浏览(196)

我想通过脚本验证域名所有权,特别是Python脚本,并想知道如何查找DNS TXT条目的值。我知道有这样的服务和网站,但我想用脚本来做。

cyvaqqii

cyvaqqii1#

使用 dnspython 很 容易 做到 这 一 点 。 下面 是 一 个 示例 :

import dns.resolver
print dns.resolver.resolve("aaa.asdflkjsadf.notatallsuspicio.us","TXT").response.answer[0][-1].strings[0]

中 的 每 一 个
这 会 产生 下列 输出 :

PnCcKpPiGlLfApDbDoEcBbPjIfBnLpFaAaObAaAaMhNgNbIfPbHkMiEfPpGgJfOcPnLdDjBeHkOjFjIbPbIoKhIjHfJlAhAhFgGbGgNlMgKmFkLgNfBjMbCoBeNbGeOnAeHgLmKoFlLhLmDcKlEdEbDpFeHkFaBlGnHiOnChIoMlIhBgOnFfKoEhDnFkKfDaMgHbJhMgPgMjGiAoJpKjKkPaIcAdGiMbIbBbAfEiKjNbCeFoElKgOePmGjJaImL

格式
另 一 个 选择 是 在 subprocess 中 使用 dig

import subprocess

print subprocess.Popen(["dig","-t","txt","aaa.asdflkjsadf.notatallsuspicio.us","+short"], stdout=subprocess.PIPE).communicate()[0]

格式

mzaanser

mzaanser2#

这可能过于简化了,但是如果您只想快速阅读TXT记录,并且不介意单独解析结果:

nslookup -q=txt somedomain.com

我发现这做了我所需要的,简短和甜蜜。

cu6pst1q

cu6pst1q3#

找到了另一种方法来获取使用dnspython的域的所有TXT记录的列表。

import dns.resolver
[dns_record.to_text() for dns_record in dns.resolver.resolve("your-domain-here", "TXT").rrset]
k5ifujac

k5ifujac4#

2022年11月20日更新

# -*- coding:utf-8 -*-
# Copyright (c) DadouLab.SIG MIT

import dns
import dns.query
import dns.resolver
import logging

logger = logging.getLogger(__name__)

class Digger(object):
    def __init__(self, resolvers=["1.1.1.1"]):
        self.mResolver = dns.resolver.Resolver()
        self.mResolver.timeout = 1
        self.mResolver.lifetime = 0.5
        self.mResolver.nameservers = resolvers
        self.spec_query_type = ['CNAME', 'TXT', 'MX', 'NS', 'SRV', 'CAA']

    def query(self, domain, query_type="A"):
        """
        answer = dns.resolver.resolve("_dnsauth.test.com", "TXT").rrset
        for dns_record in answer:
            print(dns_record.to_text())
        """
        try:
            query_type = query_type.upper()
            answer = self.mResolver.resolve(domain, query_type, raise_on_no_answer=False)
            answer_raw = answer.chaining_result.answer.to_text()
            logger.info("resolved response data => {}".format(answer_raw))
            if query_type in self.spec_query_type:
                records = [data.to_text() for data in answer]
            else:
                records = [data.address for data in answer]
            return records
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer,
                dns.resolver.NoNameservers, dns.exception.Timeout) as error:
            logger.warning("resolved error => {}".format(error))
            return

    def is_valid(self, domain, query_type="A"):
        try:
            self.mResolver.resolve(domain, query_type, raise_on_no_answer=False)
            return True
        except (dns.resolver.NXDOMAIN, dns.resolver.NoAnswer,
                dns.resolver.NoNameservers, dns.exception.Timeout) as error:
            logger.warning("resolved error => {}".format(error))
        return

if __name__ == '__main__':
    dig = Digger()
    print(dig.query("www.example.com", query_type="A"))
1dkrff03

1dkrff035#

类似这样的方法至少可以获得URL的值,我google.com在示例中使用了www.example.com。

import pycurl
import StringIO
url = "whatsmyip.us/dns_txt.php?host=google.com"
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.HTTPHEADER, ["Accept:"])
txtcurl = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, txtcurl.write)
c.perform

data = txtcurl.getvalue()
data = data.replace("Done!", "")
print data

我没有测试任何这一点,但拉它从以前的项目。
祝你好运!

相关问题