-
Notifications
You must be signed in to change notification settings - Fork 13
/
setdns
executable file
·136 lines (123 loc) · 4.55 KB
/
setdns
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env python3
# setdns -- change DNS records using DNS UPDATE
import argparse
import dns.name
import dns.rdata
from dns.rdataclass import IN
import dns.rdatatype
import dns.resolver
import ipaddress
import logging
import subprocess
def lookup_suffix(name):
ans = dns.resolver.resolve(name,
search=True,
raise_on_no_answer=False)
return ans.qname
def chase_one_cname(name):
try:
ans = dns.resolver.resolve(name, "CNAME")
except dns.resolver.NoAnswer:
return name
else:
return ans.rrset[0].target
def rtype_for_address(rdata):
addr = ipaddress.ip_address(rdata)
if addr.version == 4:
return "A"
if addr.version == 6:
return "AAAA"
raise ValueError
def send_nsupdate(zone, cmds, *, gssapi=True,
debug=False):
cmds = [f"zone {zone}\n",
*cmds,
f"send\n"]
nsupdate_args = ["nsupdate"]
if debug:
nsupdate_args += ["-d"]
if gssapi:
nsupdate_args += ["-g"]
subprocess.run(nsupdate_args,
input="".join(cmds).encode(),
check=True)
parser = argparse.ArgumentParser()
parser.add_argument("-a", "--add", action="store_true",
help="keep existing records of same type")
parser.add_argument("-r", "--remove", action="store_true",
help="remove all specified records")
parser.add_argument("-z", "--zone",
help="override automatically determined DNS zone")
parser.add_argument("-t", "--type",
help="manage records of the specified type")
parser.add_argument("-l", "--ttl", type=int, default=3600,
help="set the TTL for created records")
parser.add_argument("-n", "--dry-run", action="store_true",
help="only show updates that would be done")
parser.add_argument("-x", "--no-gss", action="store_true",
help="disable Kerberos (GSS-TSIG) authentication")
parser.add_argument("-d", "--debug", action="count", default=0,
help="enable nsupdate debugging")
parser.add_argument("-v", "--verbose", action="store_true",
help="show detailed information")
parser.add_argument("name",
help="domain name to update")
parser.add_argument("data", nargs="?",
help="record data")
args = parser.parse_args()
logging.basicConfig(level=[logging.INFO, logging.DEBUG][args.verbose],
format="%(message)s")
if "." in args.name:
rname = dns.name.from_text(args.name)
else:
print(f"Looking up \"{args.name}\"...", end=" ", flush=True)
rname = lookup_suffix(args.name)
print(f"canonicalized to <{rname}>")
if args.zone:
zone = dns.name.from_text(args.zone)
else:
print(f"Finding zone root...", end=" ", flush=True)
zone = dns.resolver.zone_for_name(rname)
print(f"starts at <{zone}>")
if args.remove and args.add:
exit("error: Options '--add' and '--remove' are mutually exclusive")
elif args.remove and args.data:
rtype = dns.rdatatype.from_text(args.type or "ANY").name
if rtype == "ANY":
exit("error: Cannot specify data with type 'ANY'")
rdata = dns.rdata.from_text(IN, rtype, args.data)
print(f"Removing {rtype} record from <{rname}>")
cmds = [f"del {rname} 0 {rtype} {rdata}\n"]
elif args.remove:
rtype = dns.rdatatype.from_text(args.type or "ANY").name
if rtype == "ANY":
print(f"Removing all records from <{rname}>")
else:
print(f"Removing all {rtype} records from <{rname}>")
cmds = [f"del {rname} 0 {rtype}\n"]
elif args.data:
if args.type:
rtype = dns.rdatatype.from_text(args.type).name
else:
rtype = rtype_for_address(args.data)
if rtype == "ANY":
exit("error: Cannot create record with type 'ANY'")
rdata = dns.rdata.from_text(IN, rtype, args.data)
if args.add:
print(f"Adding {rtype} record at <{rname}>")
cmds = [f"add {rname} {args.ttl} {rtype} {rdata}\n"]
else:
print(f"Changing {rtype} record at <{rname}>")
cmds = [f"del {rname} 0 {rtype}\n",
f"add {rname} {args.ttl} {rtype} {rdata}\n"]
else:
if args.add:
exit("error: Record data must be specified")
else:
exit("error: Either record data or '--remove' must be specified")
if args.debug >= 1:
print("Commands:")
print("\t" + "\t".join(cmds), end="")
if not args.dry_run:
send_nsupdate(zone, cmds, gssapi=not args.no_gss,
debug=(args.debug >= 2))