forked from jforman/python-ddns
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd_cname.py
More file actions
executable file
·51 lines (39 loc) · 1.72 KB
/
add_cname.py
File metadata and controls
executable file
·51 lines (39 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env python
# Example: ./add_cname.py --keyfile ddns.key --keyname ddns-key --orig puppet.test.jeffreyforman.net --dest www.example.com --server dns1
import dns.query
import dns.reversename
import dns.tsigkeyring
import dns.update
import re
import sys
import keyutils
from optparse import OptionParser
parser = OptionParser()
parser.add_option("--dest", dest="dest",
help="Destination of the CNAME.",
type="string")
parser.add_option("--orig", dest="orig",
help="Name of the CNAME.",
type="string")
parser.add_option("--keyfile", dest="key_file",
help="File containing the TSIG key.",
type="string")
parser.add_option("--keyname", dest="key_name",
help="TSIG key name to use for the DDNS update.",
type="string")
parser.add_option("--server", dest="dns_server",
help="DNS server to query.",
type="string")
parser.add_option("--ttl",dest="ttl",
help="Time to Live (in Seconds). Default: 86400",
type="int",default=86400)
(options, args) = parser.parse_args()
orig_hostname = re.search(r"(\w+).(.*)", options.orig).group(1)
orig_domain = re.search(r"(\w+).(.*)", options.orig).group(2)
print "CNAME record to be added: %s, CNAME points to: %s, DNS Server: %s" % (options.orig, options.dest, options.dns_server)
keyring = keyutils.read_tsigkey(options.key_file, options.key_name)
update = dns.update.Update(orig_domain, keyring = keyring)
update.replace(orig_hostname, options.ttl, 'CNAME', options.dest + ".")
print "--- Updating CNAME Record"
response = dns.query.tcp(update,options.dns_server)
print "Output: %s" % response