I have the following simple Python script utilising the dnspython library:

import dns.query
import dns.tsigkeyring
import dns.update
import dns.rdatatype
import datetime
import sys

# Get data
current_ip = '1.2.3.4'

# Update DNS server
keyring = dns.tsigkeyring.from_text({
    'my.key.name' : 'xxxxxxxxxxxxxxxxxxxx=='
})

update = dns.update.Update('dyn.example.com', keyring=keyring)
update.replace('myhost', 60, dns.rdatatype.A, current_ip)
update.replace('myhost', 60, dns.rdatatype.TXT, "xxx yyy zzz")

response = dns.query.tcp(update, 'ns.example.com')

It works reasonably well as evidenced by the output of the host command, but for some reason my TXT value seems to be split on each space and each segment quoted. So the output from the host command looks like the following:

$ host -t ANY myhost.dyn.example.com
myhost.dyn.example.com descriptive text "xxx" "yyy" "zzz"
myhost.dyn.example.com has address 1.2.3.4

Can anyone tell me why this is happening, and how to have it do the proper thing, which would be this:

$ host -t ANY myhost.dyn.example.com
myhost.dyn.example.com descriptive text "xxx yyy zzz"
myhost.dyn.example.com has address 1.2.3.4

I must be missing something pretty obvious, but the documentation for dnspython is a little short on examples and much Googling has not revealed anything thus far. All help gratefully received.

有帮助吗?

解决方案

I appear to have found the answer.

The value for the TXT record needs to be wrapped in quotes - i.e. the string needs to have quotes within it:

update.replace('myhost', 60, dns.rdatatype.TXT, '"xxx yyy zzz"')

Works perfectly:

$ host -t ANY myhost.dyn.example.com
myhost.dyn.example.com descriptive text "xxx yyy zzz"
myhost.dyn.example.com has address 1.2.3.4

Yay :-)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top