web123456

python notes series: DNS processing module dnspython

DNS processing module dnspython

wget /kits/1.9.4/dnspython-1.9.
tar -zxvf dnspython-1.9.
cd dnspython-1.9.
python install

DNS resolver class provided by dnspython: resolver, its query method can implement domain name query, the method definition is as follows:
query(self,qname,rdtype=1,rdclass=1,tcp=False,source=None,raise_on_no_answer=True,source_port=0)
qname is a domain name, rdtype specifies the type of RR resource, commonly used as follows:
A: Change the host name to IP
MX mail exchange record, defines the domain name of the mail server
CNAME alias record, implements mapping between domain names
Domain name server and authorized subdomain for NS tagged areas
PTR reverse parsing, opposite to A record
SOA SOA tag, a definition of a starting authorization area
rdclass Specifies the network type, optional IN (default), CH, and HS
Whether to enable TCP protocol, default False (not enabled)
source source source address, default query device IP
source_port source address port, default 0
raise_on_no_answer Specifies whether an exception is triggered when there is no response in the query. The default is True

A record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
A = (domain , 'A' )
for i in :
for j in :
print

MX record query (note that the input domain name does not include www)
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
MX = (domain , 'MX' )
for i in MX:
print 'MX preference =' , , 'mail exchanger =' ,

NS record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
ns = (domain , 'NS' )
for i in :
for j in :
print j.to_text()

CNAME record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
cname = (domain , 'CNAME' )
for i in :
for j in :
print j.to_text()

DNS domain name polling service monitoring
#!/usr/bin/python
# coding=UTF-8 #Add to this line, otherwise an error will be reported due to the existence of Chinese comments. '\xe5' in file
import
import os
import httplib
iplist=[] #Define domain name IP list variables
appdomain= "" #Define business domain name
def get_iplist (domain= "" ): #Domain name resolution function, IP will be appended to iplist after successful resolution
try :
A = (domain , 'A' ) #Analysis of record type
except Exception , e:
print "dns resolver error:" +str(e)
return
for i in :
for j in :
() #Add to iplist
return True
def checkip (ip):
checkurl=ip+ ":80"
getcontent= ""
( 5 ) #Define http connection timeout (5 seconds)
conn=(checkurl) #Create http connection object
try :
( "GET" , "/" , headers = { "Host" : appdomain}) #Start a URL request and add host header
r=()
getcontent =( 15 ) #Get the first 15 characters of the URL page for usability verification
finally :
if getcontent== "<!doctype html>" : #The content of the monitoring URL page is generally defined in advance, such as "HTTP200" and so on
print ip+ " [OK]"
else :
print ip+ " [Error]" #The alarm program can be placed here, which can be email or SMS notifications
if __name__== "__main__" :
if get_iplist(appdomain) and len(iplist)> 0 : #Condition: Domain name resolution is correct and at least one IP must be returned
for ip in iplist:
checkip(ip)
else :
print "dns resolver error."

DNS processing module dnspython

wget /kits/1.9.4/dnspython-1.9.
tar -zxvf dnspython-1.9.
cd dnspython-1.9.
python install

DNS resolver class provided by dnspython: resolver, its query method can implement domain name query, the method definition is as follows:
query(self,qname,rdtype=1,rdclass=1,tcp=False,source=None,raise_on_no_answer=True,source_port=0)
qname is a domain name, rdtype specifies the type of RR resource, commonly used as follows:
A: Change the host name to IP
MX mail exchange record, defines the domain name of the mail server
CNAME alias record, implements mapping between domain names
Domain name server and authorized subdomain for NS tagged areas
PTR reverse parsing, opposite to A record
SOA SOA tag, a definition of a starting authorization area
rdclass Specifies the network type, optional IN (default), CH, and HS
Whether to enable TCP protocol, default False (not enabled)
source source source address, default query device IP
source_port source address port, default 0
raise_on_no_answer Specifies whether an exception is triggered when there is no response in the query. The default is True

A record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
A = (domain , 'A' )
for i in :
for j in :
print

MX record query (note that the input domain name does not include www)
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
MX = (domain , 'MX' )
for i in MX:
print 'MX preference =' , , 'mail exchanger =' ,

NS record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
ns = (domain , 'NS' )
for i in :
for j in :
print j.to_text()

CNAME record query
#!/usr/bin/env python
import
domain = raw_input( 'Please input an domain: ' )
cname = (domain , 'CNAME' )
for i in :
for j in :
print j.to_text()

DNS domain name polling service monitoring
#!/usr/bin/python
# coding=UTF-8 #Add to this line, otherwise an error will be reported due to the existence of Chinese comments. '\xe5' in file
import
import os
import httplib
iplist=[] #Define domain name IP list variables
appdomain= "" #Define business domain name
def get_iplist (domain= "" ): #Domain name resolution function, IP will be appended to iplist after successful resolution
try :
A = (domain , 'A' ) #Analysis of record type
except Exception , e:
print "dns resolver error:" +str(e)
return
for i in :
for j in :
() #Add to iplist
return True
def checkip (ip):
checkurl=ip+ ":80"
getcontent= ""
( 5 ) #Define http connection timeout (5 seconds)
conn=(checkurl) #Create http connection object
try :
( "GET" , "/" , headers = { "Host" : appdomain}) #Start a URL request and add host header
r=()
getcontent =( 15 ) #Get the first 15 characters of the URL page for usability verification
finally :
if getcontent== "<!doctype html>" : #The content of the monitoring URL page is generally defined in advance, such as "HTTP200" and so on
print ip+ " [OK]"
else :
print ip+ " [Error]" #The alarm program can be placed here, which can be email or SMS notifications
if __name__== "__main__" :
if get_iplist(appdomain) and len(iplist)> 0 : #Condition: Domain name resolution is correct and at least one IP must be returned
for ip in iplist:
checkip(ip)
else :
print "dns resolver error."