#!/usr/bin/env python

import dns.resolver, sys

def get_domain_ip(domain):
"""Get the DNS record, if any, for the given domain."""
dns_records = list()
try:
# get the dns resolutions for this domain
dns_results = dns.resolver.query(domain)
dns_records = [ip.address for ip in dns_results]
except dns.resolver.NXDOMAIN as e:
print "the domain does not exist so dns resolutions remain empty. domain:", domain
except dns.resolver.NoAnswer as e:
print "the resolver is not answering so dns resolutions remain empty, domain:", domain
return dns_records hostname = sys.argv[1]
print "Recursive name lookup (simulates dig)..." n=hostname
try:
while True:
for rdata in dns.resolver.query(n, 'CNAME') :
print n, "cname is", rdata
n=rdata.target
except:
print get_domain_ip(n)

例如:

python dig_ip.py 8264.com
Recursive name lookup (simulates dig)...
8264.com cname is qaz2d84guo7uz5q2.gfnormal01at.com.
[u'121.29.18.91'] =>IP地址

python dig_ip.py www.baidu.com
Recursive name lookup (simulates dig)...
www.baidu.com cname is www.a.shifen.com.
www.a.shifen.com. cname is www.wshifen.com.
[u'103.235.46.39', u'103.235.46.40'] =>IP地址

来一个无查询结果的 DGA域名:

python dig_ip.py s09xo3-l5domek9ck5ct3go4m.com
Recursive name lookup (simulates dig)...
the domain does not exist so dns resolutions remain empty. domain: s09xo3-l5domek9ck5ct3go4m.com
[]

其中,dns.resolver.NoAnswer会在查询类别错误时候跑出此异常,例如:

python dig_ip.py www.baidu.com
Recursive name lookup (simulates dig)...
www.baidu.com cname is www.a.shifen.com.
www.a.shifen.com. cname is www.wshifen.com.
The DNS response does not contain an answer to the question: www.wshifen.com. IN CNAME

最后重构下代码:

#!/usr/bin/env python

import dns.resolver, sys

def get_domain_ip(domain):
"""Get the DNS record, if any, for the given domain."""
dns_records = list()
try:
# get the dns resolutions for this domain
dns_results = dns.resolver.query(domain)
dns_records = [ip.address for ip in dns_results]
except dns.resolver.NXDOMAIN as e:
print "the domain does not exist so dns resolutions remain empty. domain:", domain
except dns.resolver.NoAnswer as e:
print "the resolver is not answering so dns resolutions remain empty, domain:", domain
return dns_records def dig_ip(n):
try:
while True:
for rdata in dns.resolver.query(n, 'CNAME') :
print n, "cname is", rdata
n=rdata.target
except Exception as e:
print e
return get_domain_ip(n) if __name__ == "__main__":
print "Recursive name lookup (simulates dig)..."
print dig_ip(sys.argv[1])

最新文章

  1. CSS3新特性,绘制常见图形
  2. [Erlang 0108] Elixir 入门
  3. Linux中cp和scp命令的使用方法
  4. git详细教程
  5. jquery(1.3.2)<--json-->spring(3.0)
  6. 3.4.2内核下的I2C驱动
  7. loadmore & scroll
  8. DTO学习系列之AutoMapper(五)----当EntityFramework爱上AutoMapper
  9. volatile解析(转)
  10. 服务端性能测试 TPS
  11. Supercomputer 解题报告
  12. Java自动内存管理机制学习(二):垃圾回收器与内存分配策略
  13. Spring Boot 与 OAuth2 官方最详细教程
  14. BZOJ2084 [Poi2010]Antisymmetry Manachar
  15. iOS开发-UIView扩展CGRect
  16. 2018GIAC全球互联网架构大会上海站最新日程抢先看!
  17. python 正则基本方法
  18. 还在手动给css加前缀?no!几种自动处理css前缀的方法简介
  19. Swift网络封装库Moya中文手册之Targets
  20. TortoiseGit 使用 HTTP 方式每次 PUSH 无需输入密码的方法

热门文章

  1. 大数据学习——hive的sql练习
  2. hrbust-1909理工门外的树,不用线段数,贪心思路~~
  3. 【最小费用最大流】N. April Fools' Problem (medium)
  4. RedisDesktopManager 踩坑之旅
  5. T2627 村村通 codevs
  6. tomcat配置访问项目时不需要加项目名称
  7. linux 中断机制浅析
  8. Oracle冷备和热备脚本
  9. [转]图解eclipse 查看原始类出现The jar file rt.jar has no source attachment
  10. HDU 5288(OO’s Sequence-区间互质情况统计)