Search code examples
dnsdnspython

Delete specific dns record with dnspython when multiple are present


Consider following records in a dns server:

;; ANSWER SECTION:
test.example.com        3600  IN  TXT   "line1"
test.example.com        3600  IN  TXT   "line2"

Is it possible to delete 1 of the records and leave the other one?

When performing the delete with:

name = 'test.example.com.'
datatype = dns.rdatatype.from_text("TXT")
rdata = dns.rdata.from_text(dns.rdataclass.IN, dns_datatype, "line1")
update.present(name, rdata)
update.delete(name, rdata)

I get a NXRRSET (8) response.

When deleting with:

name = 'test.example.com.'
datatype = dns.rdatatype.from_text("TXT")
update.present(name, datatype)
update.delete(name, datatype)

then both TXT records are deleted (as expected).

My example is for TXT records but I have the same issue for other datatypes like A and PTR. Removing 1 of multiple records for the same name and type doesn't seem to work.

Any suggestions? Or is this not possible?

update

When using the nsupdate cli tool it seems possible. When I send following commands, only 1 of the 2 entries are removed:

$ nsupdate -v -k example.com.key
server ns.example.com
nupdate delete test.example.com. TXT line1
send

Since it's working for dns update, I'll see to make an issue on the dnspython github page


Solution

  • After testing more with nsupdate the solution became clear. When using the present prereq, I just had to add them all so the preq was satisfied. The following code uses present and only deletesthe "line1" txt record:

    name = 'test.example.com.'
    datatype = dns.rdatatype.from_text("TXT")
    rdata = dns.rdata.from_text(dns.rdataclass.IN, dns_datatype, "line1")
    rdata2 = dns.rdata.from_text(dns.rdataclass.IN, dns_datatype, "line2")
    update.present(name, rdata)
    update.present(name, rdata2)
    update.delete(name, rdata)