简介

dnspython – 是python实现的一个DNS工具包,利用其查询功能来实现dns的服务监控及解析结果的校验

安装dnspython

pip install dnspython

使用

常见的DNS解析类型包括A、MX、NS、CNAME 
(1)A记录的查询,实例如下:

import dns.resolver

domain = raw_input('Please input an domain: ')

A = dns.resolver.query(domain, 'A')
for i in A.response.answer:
for j in i.items:
print j.address

运行输入:www.baidu.com,输出结果如下: 

(2)MX记录

domain = raw_input('Please input an domain: ')

MX = dns.resolver.query(domain, 'MX')
for i in MX:
print 'MX preference =', i.preference, 'mail exchanger =', i.exchange

运行输入:163.com,输出结果如下: 

(3)NS记录

print '*************NS****************'
domain = raw_input('Please input an domain: ')
ns = dns.resolver.query(domain, 'NS')
for i in ns.response.answer:
for j in i.items:
print j.to_text()

输入:baidu.com,输出结果如下:

 
注意:只限输入一级域名,若输入www.baidu.com是错误的

(4)CNAME记录

print '****************CNAME****************'
domain = raw_input('Please input an domain: ') cname = dns.resolver.query(domain, 'CNAME')
for i in cname.response.answer:
for j in i.items:
print j.to_text()

输入:www.baidu.com,输出结果如下: 

一个综合的例子:

import dns.resolver
import httplib iplist = [] # 定义域名IP列表变量
# appdomain = "www.google.com.hk" # 定义业务域名
appdomain = "www.baidu.com" # 定义业务域名 # 域名解析函数,解析成功IP将追加到iplist
def get_iplist(domain=""):
try:
A = dns.resolver.query(domain, 'A') # 解析A记录类型
except Exception, e:
print "dns resolver error:" + str(e)
return
for i in A.response.answer:
for j in i.items:
iplist.append(j.address) # 追加到iplist
return True def checkip(ip):
checkurl = ip + ":80"
getcontent = ""
httplib.socket.setdefaulttimeout(5) # 定义http连接超时时间(5秒)
conn = httplib.HTTPConnection(checkurl) # 创建http连接对象 try:
conn.request("GET", "/", headers={"Host": appdomain}) # 发起URL请求,添加host主机头
r = conn.getresponse()
getcontent = r.read(15) # 获取URL页面前15个字符,以便做可用性校验
finally:
if getcontent.lower() == "<!doctype html>": # 监控URL页的内容一般是事先定义好,比如“HTTP200”等
print ip + " [OK]"
else:
print ip + " [Error]" # 此处可放告警程序,可以是邮件、短信通知 if __name__ == "__main__":
if get_iplist(appdomain) and len(iplist) > 0: # 条件:域名解析正确且至少要返回一个IP
for ip in iplist:
checkip(ip)
else:
print "dns resolver error."

  

python -- DNS处理模块dnspython的更多相关文章

  1. python自动化运维笔记3 —— dns处理模块dnspython

    1.3 DNS处理模块 dnspython是python实现的一个DNS工具包,它支持几乎所有的记录类型,可以用于查询.传输并动态更新ZONE信息,同时支持TSIG(事物签名)验证消息和EDNS0(扩 ...

  2. python运维开发常用模块(三)DNS处理模块dnspython

    1.dnspython模块介绍: dnspython(http://www.dnspython.org/)是Python实现的一个DNS 工具包,它支持几乎所有的记录类型,可以用于查询.传输并动态更新 ...

  3. DNS处理模块dnspython

    一.介绍 官网:http://www.dnspython.org/ https://pypi.org/project/dnspython/ dnspython是Python的DNS工具包.它支持几乎所 ...

  4. DNS 处理模块 dnspython

    简介: dnspython (http://www.dnspython.org/)是Python实现一个DNS的工具包,支持所有的记录类型,可以用于查询.传输并动态更新ZONE信息. 安装 wget ...

  5. python已安装了DNS处理模块,执行时却报错ImportError

    一.代码: #!/usr/bin/python import dns.resolver ............此处省略 二.故障报错 ubuntu:~/automation/001_base$ py ...

  6. python之platform模块

    python之platform模块 ^_^第三个模块从天而降喽!! 函数列表 platform.system() 获取操作系统类型,windows.linux等 platform.platform() ...

  7. python之OS模块详解

    python之OS模块详解 ^_^,步入第二个模块世界----->OS 常见函数列表 os.sep:取代操作系统特定的路径分隔符 os.name:指示你正在使用的工作平台.比如对于Windows ...

  8. python之sys模块详解

    python之sys模块详解 sys模块功能多,我们这里介绍一些比较实用的功能,相信你会喜欢的,和我一起走进python的模块吧! sys模块的常见函数列表 sys.argv: 实现从程序外部向程序传 ...

  9. 学习PYTHON之路, DAY 6 - PYTHON 基础 6 (模块)

    一 安装,导入模块 安装: pip3 install 模块名称 导入: import module from module.xx.xx import xx from module.xx.xx impo ...

随机推荐

  1. momentum公式

    momentum对于w的更新公式: http://caffe.berkeleyvision.org/tutorial/solver.html

  2. CPP-基础:C/C++数组名与指针的区别

    2005-08-23 08:36 来源:天极网 作者:宋宝华 责任编辑:方舟·yesky 引言 指针是C/C++语言的特色,而数组名与指针有太多的相似,甚至很多时候,数组名可以作为指针使用.于是乎,很 ...

  3. iOS 多线程编程

    参考文章: iOS多线程编程之NSThread的使用http://blog.csdn.net/totogo2010/article/details/8010231 iOS多线程编程之NSOperati ...

  4. 转 Anaconda启动卡死的解决方案

    https://blog.csdn.net/meng_zhi_xiang/article/details/83651676

  5. 【点分治】luoguP2664 树上游戏

    应该是一道中等难度的点分?麻烦在一些细节. 题目描述 lrb有一棵树,树的每个节点有个颜色.给一个长度为n的颜色序列,定义s(i,j) 为i 到j 的颜色数量.以及 现在他想让你求出所有的sum[i] ...

  6. 10GNU C语言函数调用

    6. C 函数调用机制概述 ​ 在 Linux 内核程序 boot/head.s 执行完基本初始化操作之后,就会跳转区执行 init/main.c 程序.那么 head.s 程序时如何把执行控制转交给 ...

  7. destoon 多表联合查询时出现解析错误,parse_str函数解析错误

    数据库前缀  wb_ 标签 ,调用文章时获取评论数量 <!--{php $tags=tag("table=article_24 a left join wb_comment_stat ...

  8. 有关Kali更新问题的解决方法。

    近期更新源遭遇诸多不顺,无非是各种依赖问题的报错夹杂着各种稀奇古怪的问题,不过既然是玩Linux,就要做好处理各种疑难杂症的准备.经过了这几天的不断尝试,今天终于解决了更新出错的问题. 本人更新源出现 ...

  9. Python爬虫二

    常见的反爬手段和解决思路 1)明确反反爬的主要思路 反反爬的主要思路就是尽可能的去模拟浏览器,浏览器在如何操作,代码中就如何去实现;浏览器先请求了地址url1,保留了cookie在本地,之后请求地址u ...

  10. 使用IAR在开发nordic问题记录

    使用IAR在开发nordic的sdk的时候,官方有一段话*****Note for IAR 8 users:(Libraries for IAR 8 require wchar_t to be of ...