標籤:DNS處理模組dnspython使
1 dnspython處理模組的安裝,源碼方式
wget http://www.dnspython.org/kits/1.9.4/dnspython-1.9.4.tar.gz
tar zxf dnspython-1.9.4.tar.gz
cd dnspython-1.9.4/
python setup.py install
2 解析樣本
A記錄查詢樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-import dns.resolverdomain = raw_input("請輸入一個需要解析的網域名稱:")A = dns.resolver.query(domain,'A')for i in A.response.answer: for j in i.items: if j.rdtype == 1: print j.address
MX記錄樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-import dns.resolverdomain=raw_input('請輸入一個網域名稱:')MX=dns.resolver.query(domain,'MX')for i in MX: print i.preference, i.exchange
NS記錄樣本
import dns.resolverdomain=raw_input('請輸入一個網域名稱:')NS=dns.resolver.query(domain,'NS')for i in NS.response.answer: for j in i.items: print j.to_text()
CNAME記錄樣本
import dns.resolverdomain=raw_input('請輸入一個網域名稱:')cname=dns.resolver.query(domain,'CNAME')for i in cname.response.answer: for j in i.items: print j.to_text()
3 利用dns網域名稱輪訓監控業務
監控樣本
#!/usr/bin/env python# -*- coding: utf-8 -*-import dns.resolverimport osimport httplibiplist=[]appdomain="www.ctrip.com"def get_iplist(domain=""): try: A=dns.resolver.query(domain,'A') except Exception,e: print "dns resolver error:"+str(e) for i in A.response.answer: for j in i.items: if j.rdtype == 1: iplist.append(j.address) return Truedef checkip(ip): checkurl=ip+":80" getcontent="" httplib.socket.setdefaulttimeout(5) conn=httplib.HTTPConnection(checkurl) try: conn.request("GET","/",headers={"Host":appdomain}) r=conn.getresponse() getcontent=r.read(15) finally: print getcontent if getcontent=="<!DOCTYPE html>": print ip+":80連接埠正常 [ok]" else: print ip+":80連接埠異常 [error]"if __name__=="__main__": if get_iplist(appdomain) and len(iplist)>0: for ip in iplist: checkip(ip) else: print "dns resolver error"
執行 python test1.py
<!DOCTYPE html>
118.89.206.133:80 連接埠正常 [ok]
DNS模組dnspython應用及業務監控樣本