Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1782339
  • 博文数量: 297
  • 博客积分: 285
  • 博客等级: 二等列兵
  • 技术积分: 3006
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-06 22:04
个人简介

Linuxer, ex IBMer. GNU https://hmchzb19.github.io/

文章分类

全部博文(297)

文章存档

2020年(11)

2019年(15)

2018年(43)

2017年(79)

2016年(79)

2015年(58)

2014年(1)

2013年(8)

2012年(3)

分类: Python/Ruby

2015-09-01 20:01:22

首先安装dnspython3

点击(此处)折叠或打开

  1. pip3 install dnspython3

点击(此处)折叠或打开

  1. import argparse, dns.resolver

  2. def lookup(name):
  3.     for qtype in 'A', 'AAAA', 'CNAME', 'MX', 'NS':
  4.         answer = dns.resolver.query(name, qtype, raise_on_no_answer=False)
  5.         if answer.rrset is not None:
  6.             print(answer.rrset)

  7. if __name__ == '__main__':
  8.     parser = argparse.ArgumentParser(description='Resolve a name using DNS')
  9.     parser.add_argument('name', help='name that you want to look up in DNS')
  10.     lookup(parser.parse_args().name)

点击(此处)折叠或打开

  1. import argparse, dns.resolver

  2. def resolve_hostname(hostname, indent=''):
  3.     "Print an A or AAAA record for `hostname`; follow CNAMEs if necessary."
  4.     indent = indent + ' '
  5.     answer = dns.resolver.query(hostname, 'A')
  6.     if answer.rrset is not None:
  7.         for record in answer:
  8.             print(indent, hostname, 'has A address', record.address)
  9.         return
  10.     answer = dns.resolver.query(hostname, 'AAAA')
  11.     if answer.rrset is not None:
  12.         for record in answer:
  13.             print(indent, hostname, 'has AAAA address', record.address)
  14.         return
  15.     answer = dns.resolver.query(hostname, 'CNAME')
  16.     if answer.rrset is not None:
  17.         record = answer[0]
  18.         cname = record.address
  19.         print(indent, hostname, 'is a CNAME alias for', cname) #?
  20.         resolve_hostname(cname, indent)
  21.     answer = dns.resolver.query(hostname, 'AAAA')
  22.     if answer.rrset is not None:
  23.         for record in answer:
  24.             print(indent, hostname, 'has AAAA address', record.address)
  25.         return
  26.     answer = dns.resolver.query(hostname, 'CNAME')
  27.     if answer.rrset is not None:
  28.         record = answer[0]
  29.         cname = record.address
  30.         print(indent, hostname, 'is a CNAME alias for', cname) #?
  31.         resolve_hostname(cname, indent)
  32.         return
  33.     print(indent, 'ERROR: no A, AAAA, or CNAME records for', hostname)

  34. def resolve_email_domain(domain):
  35.     "For an email address `name@domain` find its mail server IP addresses."
  36.     try:
  37.         answer = dns.resolver.query(domain, 'MX', raise_on_no_answer=False)
  38.     except dns.resolver.NXDOMAIN:
  39.         print('Error: No such domain', domain)
  40.         return
  41.     if answer.rrset is not None:
  42.         records = sorted(answer, key=lambda record: record.preference)
  43.         for record in records:
  44.             name = record.exchange.to_text(omit_final_dot=True)
  45.             print('Priority', record.preference)
  46.             resolve_hostname(name)
  47.     else:
  48.         print('This domain has no explicit MX records')
  49.         print('Attempting to resolve it as an A, AAAA, or CNAME')
  50.         resolve_hostname(domain)

  51. if __name__ == '__main__':
  52.     parser = argparse.ArgumentParser(description='Find mailserver IP address')
  53.     parser.add_argument('domain', help='domain that you want to send mail to')
  54.     resolve_email_domain(parser.parse_args().domain)


阅读(803) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~