2015-06-04 97 views
0

我想通過python代碼以編程方式創建dns TXT記錄類型。
我沒有找到有關如何做的數據或TXT記錄的特定格式。如何創建DNS TXT記錄?

這裏是我試圖用Python創建TXT記錄:

# copy the id of the request 
    packet+=self.data[:2] 
    # Add the flags of response 
    packet+= "\x81\x80" 
    # Questions and Answers Counts 
    packet+='\x00\x01' 
    packet+='\x00\x01' 

    #No records follow 
    packet+='\x00\x00' 

    # Additional records follow 
    packet+='\x00\x00' 
    #packet+='\x00\x01' 

    (urlList,urlDataLength) = DecodeQuery(self.data) 
    urlPosition = urlDataLength 

    # Original Domain Name Question 
    packet+=self.data[12:12+urlDataLength+4] 

    # Pointer to domain name 
    packet+='\xc0\x0c'            

    #response type ~~~~~~~~~~~~~~~~ 
    # Response type (TXT) 
    packet+='\x00\x10' 

    #CLASS 
    packet+='\x00\x01' 
    #TTL 
    packet+='\x00\x00\x02\x58' 
    #Data Length 
    packet+='\x00\x04' 
    #txt length 
    packet+='\x04' 
    packet+= 'hi'.encode('hex') 
+0

這個? http://www.zytrax.com/books/dns/ch8/txt.html –

+0

你想達到什麼目的?在您的腳本中編寫TXT記錄有什麼好處?你想在Python中寫一個DNS服務器嗎?因爲只有DNS服務器正在編寫帶有回覆記錄的DNS數據包...... –

回答

0

dnslib(dns.py)

class TXT(RD): 
    """ 
     DNS TXT record. Pass in either a single string, or a tuple/list of strings. 

     >>> TXT('txtvers=1') 
     "txtvers=1" 
     >>> TXT(('txtvers=1',)) 
     "txtvers=1" 
     >>> TXT(['txtvers=1',]) 
     "txtvers=1" 
     >>> TXT(['txtvers=1','swver=2.5']) 
     "txtvers=1","swver=2.5" 
     >>> a = DNSRecord() 
     >>> a.add_answer(*RR.fromZone('example.com 60 IN TXT "txtvers=1"')) 
     >>> a.add_answer(*RR.fromZone('example.com 120 IN TXT "txtvers=1" "swver=2.3"')) 
     >>> print(a) 
     ;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: ... 
     ;; flags: rd; QUERY: 0, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 0 
     ;; ANSWER SECTION: 
     example.com.   60  IN  TXT  "txtvers=1" 
     example.com.   120  IN  TXT  "txtvers=1" "swver=2.3" 
    """