如何使用Python或Bash删除动态DNS记录?

时间:2013-05-09 15:09:13

标签: python dns

python更新中的以下代码

import dns.query
import dns.tsigkeyring
import dns.update
import sys
 keyring = dns.tsigkeyring.from_text({'host-example.' : 'XXXXXXXXXXXXXXXXXXXXXX=='})

update = dns.update.Update('dyn.test.example', keyring=keyring)
update.replace('host', 300, 'a', sys.argv[1])

 response = dns.query.tcp(update, '10.0.0.1')

但我找不到如何删除dns条目。

2 个答案:

答案 0 :(得分:3)

dns.update.Update import dns.query import dns.tsigkeyring import dns.update keyring = dns.tsigkeyring.from_text({'host-example.' : 'XXXXXXXXXXXXXXXXXXXXXX=='}) update = dns.update.Update('dyn.test.example', keyring=keyring) update.delete('host', 'A') response = dns.query.tcp(update, '10.0.0.1') 方法可用于删除记录。

{{1}}

答案 1 :(得分:1)

dns.update.Update 的删除方法始终将名称作为第一个参数。其他参数可以是:

  • 没有
  • rdataset ...
  • RDATA ...
  • rdtype,[string ...]

来源:http://www.dnspython.org/docs/1.15.0/

从域_acme-challenge.example.com删除特定的TXT记录,内容为“Hello World”:

import dns.resolver
import dns.tsigkeyring
import dns.update
import dns.query

# Create the rdata object
txt_record = dns.rdata.from_text(dns.rdataclass.IN, dns.rdatatype.TXT, "Hello World")

# Initialize a new DNS update object
update = dns.update.Update('example.com', keyring=keyring)

# Delete the specific record passing in the rdata object
update.delete('_acme-challenge', txt_record)

# Send the request
reponse = dns.query.udp(update, '10.0.0.1', timeout=10)

# Check if the TXT record was successfully removed:
answers = dns.resolver.query('_acme-challenge.example.com', 'TXT')
txt_records = [txt_record.strings[0] for txt_record in answers]
if "Hello World" in txt_records:
    print "TXT record not successfully deleted"
else:
    print "TXT record successfully deleted"